私は本当にAngularJsで新しく、別の$http
リクエストで複数のルートを作成しようとしています。私の問題は、ルートが変更され、ページの内容が後で表示されるときに始まります。 私は何らかの方法でそれを理解し、私はそれが正しい方法ではないと考えます。 より良い方法があるかどうか誰かが私に教えてくれることを願っています。すべてのルートでhttpを呼び出す正しい方法AngularJs
注: AngularJsバージョン:1.6 | UIのルータを使用して
main.js
var asTwl = angular.module('asTwl', ['ui.router']); asTwl.controller('generals', function($scope, $http, $timeout){ $scope.pageLoader = false; $scope.getPageData = function(path, postData, obj){ $scope.pageLoader = true; $http({ method: 'post', url: path, data: postData, headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }) .then(function(response) { if (response.data) { $scope.data[obj] = JSON.parse(response.data); $timeout(function(){ $scope.pageLoader = false; }, 100) } }) .catch(function(e) { new Error('Error: ', e); throw e; }) } }); asTwl.controller('homePage', function($scope, $http){ var postData = { //data... } $scope.getPageData('path', postData, 'home') }) asTwl.controller('singlePage', function($scope, $http, $stateParams){ var postData = $stateParams; $scope.getPageData('path', postData, 'page') }) asTwl.controller('categoryPage', function($scope, $http, $stateParams){ var postData = $stateParams; $scope.getPageData('path', postData, 'category') }) asTwl.config(function($stateProvider, $urlRouterProvider, $locationProvider){ $urlRouterProvider.otherwise('/'); $stateProvider .state('home', { url: '/', templateUrl : 'templates/pages/home.html', controller : 'homePage' }) .state('info', { url: '/info/:id', templateUrl : 'templates/pages/info.html', controller : 'singlePage' }) .state('category', { url: '/category/:type/:id', templateUrl : 'templates/pages/category.html', controller : 'categoryPage' }) });
ありがとうございました!
角度バージョン?これはUIルーターのようにも見えます。それで質問に指定してください – alonisser
私の質問を編集してください –