2015-09-21 26 views
0

我有兩個使用相同模板但具有不同控制器的狀態。該模板是一種形式 - 一種狀態是空的形式,另一種是已經填充的形式。我遇到了一個從其他狀態調用的控制器的問題。UI-router - 爲什麼這個控制器被調用?

這些狀態位於單獨的模塊中,都包含在名爲'products'的模塊中。

這是我爲創建屏幕代碼:

angular.module('products.create', []) 
    .config(function($stateProvider, $urlRouterProvider){ 
    $stateProvider 
    .state('store.products.create', { 
     url: '/create', 
     parent: 'store.products', 
     views: { 
     '[email protected]': { 
      templateUrl: 'app/products/create/subhead.tmpl.html' 
     }, 
     '[email protected]': { 
      templateUrl: 'app/products/create/create.tmpl.html', 
      controller: 'ProductCreateCtrl as controller' 
     } 
     } 
    }); 
    }) 
    .controller('ProductCreateCtrl', function() { 

    var controller = this; 

    // ONE INSTANCE OF THE CONTROLLER 
    }); 

這裏是代碼編輯/顯示屏幕:

angular.module('products.show', []) 
    .config(function($stateProvider, $urlRouterProvider){ 
    $stateProvider 
     .state('store.products.show', { 
     url: '/:productId', 
     parent: 'store.products', 
     views: { 
      '[email protected]': { 
      templateUrl: 'app/products/show/subhead.tmpl.html' 
      }, 
      '[email protected]': { 
      templateUrl: 'app/products/create/create.tmpl.html', 
      controller: 'ProductShowCtrl as controller' 
      } 
     } 
     }); 
    }) 
    .controller('ProductShowCtrl', function($stateParams, ProductService) { 

    var controller = this; 

    //THIS IS BEING CALLED IN BOTH CASES FOR SOME REASON 
    }); 

爲什麼顯示控制器被稱爲在這兩種情況?對於不同的狀態使用相同的模板有沒有問題?

這裏UI視片:

<div ng-controller="ProductsCtrl"> 
     <div ui-view="subhead"></div> 
     <div ui-view="content"></div> 
    </div> 
+0

你有沒有試過限制你的觀點到他們的國家?例如「content @ store.products.show」和「content @ store.products.create'?這樣你就可以確保他們不會填充其他視圖。 – fixmycode

+0

這是行不通的。他的目標是在根模板中使用ui-view。 –

+0

@HellaMedusa你怎麼知道你的其他控制器正在被調用?你沒有在模板中使用ng-controller,對吧? –

回答

0

在類似的東西找到萬一別人絆倒的回答我的問題:該URL正在匹配兩次。我沒有將任何正則表達式應用於productId,因此/ create被解釋爲有效的產品ID。

相關問題