1
我只想在控制器「數據控制器」中獲取影片變量的值。下面是我對app.js代碼片段:無法從控制器內的服務中獲取值
myApp.service('datatransfer', ['$http', function($http)
{
var service = this;
var movies;
service.getMovies = function()
{
$http.get('https://api.myjson.com/bins/54jf7').success(function(data)
{
movies = data;
});
return movies;
};
}]);
myApp.controller('DataController', ['$scope', '$http', '$location', 'datatransfer', function($scope, $http, $location, datatransfer)
{
$scope.loaded = false;
datatransfer.getMovies().then(function(data)
{
$scope.movies = data;
$scope.loaded = true;
});
$location.path('/home');
}]);
從服務「數據傳遞」,它返回影片的正確值,但是當服務被注入控制器和getMovies()被使用,那麼它給低於錯誤。它無法佔用getMovies()返回的值。
angular.js:13642 TypeError: Cannot read property 'then' of undefined
at new <anonymous> (app.js:47)
at Object.invoke (angular.js:4708)
at P.instance (angular.js:10177)
at n (angular.js:9096)
at g (angular.js:8459)
at g (angular.js:8462)
at angular.js:8339
at angular.js:1782
at m.$eval (angular.js:17378)
at m.$apply (angular.js:17478)
在此先感謝。
呀,「」然後「」應該與$承諾,是用來返回HTTP請求
捕捉到控制器的承諾由$ http.get(...)返回。 – Maxwellt
嘿@sachila,謝謝,現在工作。 –
@Kunal Tyagi不錯。請務必檢查答案,如果這有幫助:D –