使用$ filter服务时出现未知提供程序错误

时间:2016-07-06 18:23:35

标签: angularjs filter ionic-framework runtime-error

我正在使用Ionic框架创建一个混合应用程序,并且我建立了一个工厂来保存一个对象数组。我想使用$ filter服务来获取具有特定ID的数组中的对象。这是我现在的代码。我从这个堆栈溢出问题中得到了这个实现的想法:In Angular, I need to search objects in an array

angular.module('prototype.services', [])

.factory('Patients', function($filter) {

var currentID;
var currentPatient = $filter('patientFilter')(patients, {id: currentID}, true);


var patients = [{
id: 1,
name: 'Alexander Hamilton'
}];



return {

all:function() {
    return patients;
},

add: function(patient) {
    patients.push( {
        name: patient.name
    })
},

setID: function(id) {
    currentID = id;
},

getID: function() {
    return currentID;    
},


};



});

据我所知,这应该有效,因为我将$ filter注入我创建的工厂。我对角度很新,所以可能会有一些我不知道的东西。

完整错误是:

ionic.bundle.js:26794 Error: [$injector:unpr] Unknown provider:     patientFilterFilterProvider <- patientFilterFilter <- Patients
http://errors.angularjs.org/1.5.3/$injector/unpr?    p0=patientFilterFilterProvider%20%3C-%20patientFilterFilter%20%3C-%20Patients
at ionic.bundle.js:13438
at ionic.bundle.js:17788
at Object.getService [as get] (ionic.bundle.js:17941)
at ionic.bundle.js:17793
at Object.getService [as get] (ionic.bundle.js:17941)
at ionic.bundle.js:32697
at Object.<anonymous> (services.js:6)
at Object.invoke (ionic.bundle.js:17995)
at Object.enforcedReturnValue [as $get] (ionic.bundle.js:17834)
at Object.invoke (ionic.bundle.js:17995)

1 个答案:

答案 0 :(得分:0)

您的代码假设您的模块附加了patientFilter

angular.module('prototype.services', [])

.filter('patientFilter', function(){/*...*/}) // doesn't exist, so unknown provider errors

您可能想要做的是以下......

angular.module('prototype.services', [])

.factory('Patients', function($filter) {

    var currentID;
    var patients = [{
        id: 1,
        name: 'Alexander Hamilton'
    }];

    return {

      // ... other factory methods ...

      getCurrentPatient: function(){
        var currentPatient = $filter('filter')(patients, { // use generic 'filter' filter
            id: currentID
        }, true);

        return currentPatient || patients[0];
      }
    }
});