2015-11-11 129 views
0

我有角的ui路由。 比如我有電影控制器,有我有類方法,當用戶進入到/電影/類別我想調用這個方法AngularJs ui路由 - 控制器的方法

.state('movies', { 
    url: '/movies', 
    views: { 
     'pageContent': { 
      controller: 'movies', 
      templateUrl: 'movies.html' 
     } 
    } 
}) 

如何能夠做到這樣的事:

.state('moviesCategories', { 
    url: '/movies/categories', 
    views: { 
     'pageContent': { 
      controller: 'movies', 
      **method: 'categories',** 
      templateUrl: 'categories.html' 
     } 
    } 
}) 

回答

0

如果你想執行的路線改變方法,你可以插入此代碼到電影CONTROLER categories.html

<div data-ng-init="categories();"> ... </div> 
0

應該增加$ state.go(「moviesCategories」 );此方法重定向到狀態moviesCategories

樣品中文檔

.state('state1', { 
    url: "/state1", 
    templateUrl: "partials/state1.html" 
}) 
.state('state1.list', { 
    url: "/list", 
    templateUrl: "partials/state1.list.html", 
    controller: function($scope) { 
    $scope.items = ["A", "List", "Of", "Items"]; 
    } 
}) 
+0

我若** /電影/分類/:塞**的路線,我怎麼能猜出哪個方法必須執行? – zarandaaa

+0

我編輯答案。但一個不正確的理解單詞「方法」?你想在控制器'電影'中調用函數? –

0

更好創建爲每個路由單獨的控制器。讓我們說'categoriesController'。

$routeProvider 
    .when('/movie',{ 
     templeteUrl:'templates/movies', 
     controller:'moviesController' 
    ) 
    .when('/movies/categories',{ 
     templeteUrl:'templates/movieCategories', 
     controller:'movieCategoryController', 
     resolve:{ 
       categories://pull categories using a service here. inside service write $http call.   
     } 
    ); 

內部控制

//will inject the categories we got in reolve 
angular.controller('movieCategoryController',['categories','$scope',functions(categories,$scope){ 

    $scope.categories=categories; 
}]);