2016-08-11 6 views
0

次のコードを書くにはどうすればよいでしょうか?私はちょうど以下の "when"のショートカットが必要です。私は100回「いつ」を書くのか嫌いです。あなたが代わりにもっときれいにこれを達成するためにui-routerモジュールを使用することができます角度js ng-routeを使用して効率的にページを書く方法

var module = angular.module("sampleApp", ['ngRoute']); 

module.config(['$routeProvider', 
    function($routeProvider) { 
     $routeProvider. 
      when('/route1', { 
       templateUrl: 'angular-route-template-1.jsp', 
       controller: 'RouteController' 
      }). 
      when('/route2', { 
       templateUrl: 'angular-route-template-2.jsp', 
       controller: 'RouteController' 
      }). 
      otherwise({ 
       redirectTo: '/' 
      }); 
    }]); 

module.controller("RouteController", function($scope) { 

}) 

答えて

0
module.config(['$routeProvider', 'routes' 

    function($routeProvider, routes) { 
    // routes is the list of my routes 
    // e.g. 
    // routes = [{ 
    // url: '/about', 
    // options: { 
    //  templateUrl: '/about.html', 
    //  controller: 'AboutController as vm', 
    // } 
    // }] 

    routes.forEach(function(route) { 
     $routeProvider.when(route.url, route.options); 
    }); 

    $routeProvider. 
    otherwise({ 
     redirectTo: '/' 
    }); 
    } 
]); 
+0

( '/約'、{ templateUrl: 'about.jsp' })。 when( '/ contact'、{ templateUrl: 'contact.jsp' })。 – sibaspage

+0

のような "about"と "contact"私は別の名前で複数のテンプレートを追加したい。だから私はすぐに上記のコードを書くための方法はありますか質問したい。私は100回書く必要はありません。 – sibaspage

+0

あなたはこのルートリストをどこかに置いておく必要があります。そうでなければ、どのようにループを作成できますか?以前のサーバーへのリクエストでもかまいませんが、リストから開始する必要があります –

0

var myApp = angular.module('helloworld', ['ui.router']); 

myApp.config(function($stateProvider) { 
    var helloState = { 
    name: 'hello', 
    url: '/hello', 
    template: '<h3>hello world!</h3>' 
    } 

    var aboutState = { 
    name: 'about', 
    url: '/about', 
    template: '<h3>Its the UI-Router hello world app!</h3>' 
    } 

    $stateProvider.state(helloState); 
    $stateProvider.state(aboutState); 
}); 

あなたは(おそらく、独自のjsファイル内の)配列内のあなたの100「状態」オブジェクトを保存することができ、かつ$stateProvider.state(stateObject);をループして。

関連する問題