2017-06-13 67 views
0

我下面这个教程我在Stormpath找到。
我想了解如何AngularJS的作品,但我没有得到编辑功能(控制器)运行。我总是得到类型错误为什么我在angularjs中没有得到函数错误?

TypeError: SearchService.fetch is not a function 

内的调用堆栈它引用EditController在这行代码指向:

SearchService.fetch($stateParams.id, function (response) { 

这里是整个代码EditController

(function() { 
    'use strict'; 

    angular 
     .module('myApp') 
     .controller('EditController', EditController); 

    EditController.$inject = ['SearchService', '$stateParams']; 

    function EditController(SearchService, $stateParams) { 
     var vm = this; 

     SearchService.fetch($stateParams.id, function (response) { 
      vm.person = response; 
     }); 
    } 


})(); 

但是我有n个o这里有什么问题。我想比较这代码的代码为SearchController - 请参阅下文,

(function() { 
    'use strict'; 

    angular 
     .module('myApp') 
     .controller('SearchController', SearchController); 

    SearchController.$inject = ['SearchService']; 

    function SearchController(SearchService) { 
     var vm = this; 

    vm.search = function(){ 
      SearchService.query(vm.term, function (response) { 
       var results = response.filter(function (item) { 
        return JSON.stringify(item).toLowerCase().includes(vm.term.toLowerCase()); 
       }); 
       vm.searchResults = results; 
      }); 
    } 
    } 
})(); 

这里是SearchService代码:

(function() { 
    'use strict'; 

    angular 
     .module('myApp') 
     .factory('SearchService', SearchService); 

    SearchService.$inject = ['$resource']; 

    function SearchService($resource) { 
     return $resource('/api/search/people.json'); 
    } 

    SearchService.fetch = function (id, callback) { 
     Search.query(function (response) { 
      var results = response.filter(function (item) { 
      return item.id === parseInt(id); 
      }); 
      return callback(results[0]); 
     }); 
    }; 

})(); 

任何一条建议表示赞赏,我已经花了几天时间尝试各种事情。

+0

看到此代码,我们甚至不知道'SearchService'是一种服务。 (你在哪儿取指定义?) – ippi

+0

为你的错误指向的是SearchService.fetch不是一个函数....未在任何地方定义,但您尝试使用它.... IAM怀疑,美需要giveSearchService.search而不是SearchService.fetch –

+0

您是否已将search.service.js链接到您的html文件。它必须是你的app.js文件之后的链接。 –

回答

1

使搜索服务这样的..

The service factory function generates the single object or function that represents the service to the rest of the application. The object or function returned by the service is injected into any component (controller, service, filter or directive) that specifies a dependency on the service

https://docs.angularjs.org/guide/services

(function() { 
'use strict'; 

angular.module('myApp') 
    .factory('SearchService', SearchService); 

SearchService.$inject = ['$resource']; 

function SearchService($resource, $http) { 
var service = {}; 
service.url = $resource('/api/search/people.json'); 
var req = { 
method: 'GET', 
url: 'http://example.com', 
headers: { 
    'Content-Type': undefined 
}, 
data: { test: 'test' } 

} 

service.fetch = function (id, callback) { 
// $http.get('yourapi.json').then() you can try like this also 
     return $http(req).then(function (response) { 
     var results = response.filter(function (item) { 
     return item.id === parseInt(id); 
     }); 
     return callback(results[0]); 
    }); 
}; 
    return service; 
} 
})(); 
+0

感谢您的贡献和建议。我试过它作为你写的,但现在我得到的错误:类型错误:SearchService.query不是一个函数... – ivo

+0

Search.query?搜索是服务?你注射了吗? –

+0

那么,我没有在SearchController中做任何更改;我刚刚用你在答案中写下的代码替换了原来的代码。 – ivo

相关问题