[英]AngularJS ng-route how to make http request on resolve
我有一個帶有輸入和錨點鏈接的簡單模板,按下該鏈接將加載另一個模板。 我希望第二個模板通過http請求獲取信息。
我默認使用ng-route重定向到搜索模板,並從路徑/ search /:title重定向到結果模板,並且我試圖在加載結果模板之前使用resolve發出請求( 代碼在plunker中 )
我面臨的主要問題是,當我添加resolve時,控制器將停止初始化(我猜它將在返回promise后加載)。 這意味着當我在控制器搜索功能上打印變量時,諸如搜索URL之類的變量未初始化,並且$ routeParams為空。
我應該怎么做?
我也不確定解析的正確語法。 第一次搜索是一個范圍變量嗎?
resolve: {
search: searchController.search
}
對於那些懶於檢查Plunker的人,這里是相關代碼:
路由
.config(['$routeProvider',function($routeProvider) {
$routeProvider.
when('/search', {
templateUrl: 'templates/search.html'
}).
when('/search/:title', {
templateUrl: 'templates/result.html',
controller: 'searchController',
resolve: {
search: searchController.search
}
}).
otherwise({
redirectTo: '/search'
});
}]);
searchController
var searchController = search.controller('searchController', ["$http", "$scope", "$location", '$rootScope', '$routeParams', function($http, $scope, $location, $rootScope, $routeParams) {
console.log("Controller constructor");
this.searchURL = 'http://www.myapifilms.com/imdb?title=%thetitle%&format=JSON';
$scope.response = '<Response here>';
this.title = '';
this.defer = null;
console.log("PARAMS: " + JSON.stringify($routeParams));
}]);
searchController.search = function searchMovie($q, $routeParams) {
searchController.defer = $q.defer;
var searchString = $routeParams.title;
url = searchController.searchURL.replace('%thetitle%', searchString);
console.log("URL: " + url);
searchController.searchRequest(url);
};
searchController.searchRequest = function(url) {
$http.get(url).success(function(data) {
....
searchController.defer.resolve();
return searchController.defer.promise;
})
.error(function(data, status, headers, config) {
...
searchController.defer.resolve();
return searchController.defer.promise;
})
};
我認為您應該使用.factory創建服務並進行相應的編輯:
angular.module('myApp', ['ionic', 'ngRoute', 'starter', 'wc.Directives'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if (window.StatusBar) {
StatusBar.styleDefault();
}
})
})
.factory("search", function($q, $http){
return {
getMessage: function(){
//return $q.when("Response");
var promise = $http({ method: 'GET', url: 'your_url'}).success(function(data, status, headers, config) {
return data;
});
return promise;
}
};
})
.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/search', {
templateUrl: 'search.html'
}).
when('/search/:title', {
templateUrl: 'result.html',
controller: 'searchController',
/*resolve: {
search: searchController.search
}*/
resolve: {
search: function(search){
return search.getMessage();
}
}
}).
otherwise({
redirectTo: '/search'
});
}
]);
您的朋克派生者: http ://plnkr.co/edit/Ry4LAl?p=preview
我希望您能得到想要的東西,在為他人服務后,將您的朋克分享給大家。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.