1

我敢肯定这件事情简单,我在这里失踪,但我不能得到这个工作.. 我想用一个工厂,这样我可以重复使用多个控制器的数据。不能得到restangular工厂上班

(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; 
     }); 
     } 
    } 
})(); 

而且我不断收到 “类型错误:无法读取的未定义的属性 'getFriendList'”

编辑:我曾尝试为好,https://github.com/mgonto/restangular#decoupled-restangular-service,但没有运气!

+0

请形成你的标题一个实际的问题。它看起来像标签。另外,请扩展您自己解决这个问题的努力。你检查过第一/第三方图书馆的文件吗? – onebree

+0

你能提供一个plunker文件吗?我们需要知道另一个细节,例如:“您拨打服务的方式”等。 –

回答

1

您的工厂没有正确定义。为了给服务使用者提供工厂函数,您应该在函数中定义工厂代码,并返回该工具将帮助您继续承诺链。

代码

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; 
     }); 
     } 
    } 
})(); 
+0

使用此代码获取相同的错误..? – Mackelito

+0

另外,我发现这一点,http://stackoverflow.com/questions/22496041/angularjs-restangular-call-inside-factory-service,所以它看起来像它应该工作!(?) – Mackelito

+0

@Mackelito检查我的更新答案..谢谢:) –

0

你必须注入控制器的功能,里面的profileFactory服务。