Angularjs 不能让餐厅的工厂运转
我肯定我在这里错过了一些简单的东西,但我无法让它发挥作用。。 我想使用工厂,以便在多个控制器中重用数据Angularjs 不能让餐厅的工厂运转,angularjs,factory,restangular,angular-promise,angularjs-factory,Angularjs,Factory,Restangular,Angular Promise,Angularjs Factory,我肯定我在这里错过了一些简单的东西,但我无法让它发挥作用。。 我想使用工厂,以便在多个控制器中重用数据 (function() { 'use strict'; angular .module('www') .factory('profileFactory', profileFactory); profileFactory.$inject = ['Restangular']; /* @ngInject */ function profileFactory(Restangula
(function() {
'use strict';
angular
.module('www')
.factory('profileFactory', profileFactory);
profileFactory.$inject = ['Restangular'];
/* @ngInject */
function profileFactory(Restangular) {
var service = {
getUserData: Restangular.one('/user/profile/').getList(),
getFriendList: Restangular.all('api/users/getfriendsinvitations/').getList()
};
return service;
}
})();
控制员:
(function() {
'use strict';
angular
.module('www')
.controller('ProfileController', ProfileController);
ProfileController.$inject = ['profileFactory'];
/* @ngInject */
function ProfileController() {
activate();
function activate(profileFactory, $scope) {
profileFactory.getFriendList.then(function (homeFriends) {
$scope.homeFriends = homeFriends;
});
}
}
})();
我不断得到“TypeError:无法读取未定义的属性'getFriendList'”
编辑:我也试过这个,但是运气不好 您必须将profileFactory服务注入控制器的功能中。您的工厂定义不正确。为了向服务消费者提供工厂功能,您应该在函数中定义代码工厂,并返回承诺将帮助您继续承诺链 代码
function profileFactory(Restangular) {
var service = {
getUserData: function(){
return Restangular.one('/user/profile/').getList();
},
getFriendList: function(){
return Restangular.all('api/users/getfriendsinvitations/').getList();
}
};
return service;
}
控制器
(function() {
'use strict';
angular
.module('www')
.controller('ProfileController', ProfileController);
ProfileController.$inject = ['profileFactory', '$scope'];
/* @ngInject */
function ProfileController(profileFactory, $scope) { //<==added dependancy here
activate();
function activate() {
profileFactory.getFriendList().then(function (homeFriends) {
$scope.homeFriends = homeFriends;
});
}
}
})();
(函数(){
"严格使用",;
有棱角的
.module('www')
.控制器(“ProfileController”,ProfileController);
ProfileController.$inject=['profileFactory','$scope'];
/*@ngInject*/
函数ProfileController(profileFactory,$scope){//请在标题中形成一个实际问题。它看起来像标签。此外,请详述您自己为解决此问题所做的努力。您是否查看了第一方/第三方库的文档?您能提供plunker文件吗?我们需要了解其他详细信息,如:“您调用服务的方式”,等等。我还发现了这个,所以它看起来应该可以工作!(?)没有区别。。仍然是相同的错误,但这也添加了一个jshint错误…我已将“profileFactory”注入到activate()中函数,所以这不应该是问题。@Mackelito从激活函数中删除profileFactory,$scope
。。一定要看editAh。。你是对的!:)现在我得到了“无法读取未定义的属性”所以这是另一个问题:)@Mackelito看看更新答案,它只是简单地更改为profileFactory.getFriendList()
添加了函数括号