2017-06-13 65 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

相關問題