【发布时间】:2016-05-18 13:52:41
【问题描述】:
工厂/服务:
angular.module('phoenix-template')
.factory('User', ['$http', function($http){
function current(){
$http.get('http://localhost:4000/api/current_user').then(function(res){
return res.data;
})
}
this.currentUser = current();
}]);
})();
控制器:
(function(){
'use strict';
angular.module('phoenix-template')
.controller('NavCtrl', ['$scope','User', function($scope, User){
$scope.currentUser = User.currentUser;
$scope.$watch( 'currentUser', function () {
User.currentUser = $scope.currentUser;
});
}]);
})();
我正在后端跟踪当前用户,但我想跨控制器以角度跟踪我的当前用户用于显示等目的。
但是,我显然做错了..
提示,帮助,非常感谢。
【问题讨论】:
-
您没有向工厂中的
current函数返回任何内容。此外,您不会向工厂构造函数返回任何内容。
标签: angularjs angularjs-service angularjs-controller