控制器从工厂收到未定义的调用函数

代码包含两个控制器teacherrenderteacher控制器在工厂share设置值,并从工厂render调用get()函数以接收$scope.maincode值,但是console.log($scope.maincode)显示为undefined。 所以,HTML页面不会呈现{{m.code}}值。 以下是相关的片段。

控制器1

 app.controller("teacher",function($rootScope,$scope,$http,$location,$window,share){ $scope.load = function(){ $http.get('/getcode').success(function(data){ if(data.length>0){ $scope.message = ""; $scope.codes = data; console.log($scope.codes); } else { $scope.codes=""; $scope.message = "No code submissions yet"; } }) $scope.send = function(id){ console.log(id); $http.get("/code/"+id).success(function(data){ share.set(data); $window.location.href ='/code'; }) } }); 

控制器2

 app.controller('render',function($rootScope,$scope,share){ $scope.renders = function(){ $scope.m=share.get(); console.log($scope.m); } }); 

 app.factory('share',function(){ var data = {}; this.set = function(obj){ this.data = obj; console.log(this.data); } this.get = function(){ return this.data; } return { set: this.set, get: this.get } 

});

HTML文件

 <!DOCTYPE html> <html> <head> <base href="/" /> <script src="js/angular.min.js"></script> <script src="js/angular-route.min.js"></script> <script src="js/angular-resource.min.js"></script> <script src="js/app.js"></script> </head> <body ng-app="app" ng-controller="render"> <div data-ng-init="renders()"> {{m.code}} </div> <a href="/logout">Logout</a> </body> </html> 

HTML文件2(调用教师控制器)

 <!DOCTYPE html> <html> <head> <base href="/" /> <script src="/js/angular.min.js"></script> <script src="/js/angular-route.min.js"></script> <script src="/js/angular-resource.min.js"></script> <script src="/js/app.js"></script> </head> <body ng-app="app"> <div data-ng-controller="teacher"> <div data-ng-init="load()"> <div ng-repeat="code in codes" ng-show="codes"> <a href ng-click="send(code.id)">{{code.title}}</a> </div> </div> <div> <h4>{{message}}</h4> </div> </div> <a href="/logout">Logout</a> </body> 

我search了这个问题,阅读了有关类似问题的现有问题,但找不到解决scheme。 请帮忙。

由于您使用的是两个不同的页面,因此您需要使用某个路由器,例如ngRouterui-router ,或者使用localStorage 。 这里是使用localStorage的例子:

 app.factory('share', function () { function set(obj) { localStorage.setItem('share', obj); } function get() { return localStorage.getItem('share'); } return { set: set, get: get } }); 

在显示renderm.code时,您没有从承诺中得到结果。

为了克服这个问题,只要在teacher控制器的获取请求中获得了价值,就向范围broadcast

 `$rootScope.$broadcast('data-received');` 

现在,你可以在你的渲染controller中听这个事件

 $scope.$on('data-received', function(event, args) { $scope.m=share.get(); }); 

控制器1

 app.controller("teacher",function($rootScope,$scope,$http,$location,$window,share){ $scope.send = function(id){ console.log(id); $http.get("/code/"+id).success(function(data){ share.set(data); $rootScope.$broadcast('data-received'); $window.location.href ='/code'; }) } }); 

控制器2

 app.controller('render',function($rootScope,$scope,share){ $scope.renders = function(){ $scope.m=share.get(); console.log($scope.m); } $scope.$on('data-received', function(event, args) { $scope.m=share.get(); }); }); 

这应该可以解决你的问题。