UI路由器 - 为什么要调用此控制器?

时间:2015-09-21 22:24:27

标签: angularjs angular-ui-router

我有两种使用相同模板但具有不同控制器的状态。模板是一个表单 - 一个状态是空表单,另一个是已经填写的表单。我从其他状态调用其中一个控制器时出现问题。

这些状态位于单独的模块中,两者都包含在名为' products'的模块中。

以下是我创建屏幕的代码:

angular.module('products.create', [])
  .config(function($stateProvider, $urlRouterProvider){
    $stateProvider
    .state('store.products.create', {
      url: '/create',
      parent: 'store.products',
      views: {
        'subhead@': {
          templateUrl: 'app/products/create/subhead.tmpl.html'
        },
        'content@': {
          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: {
          'subhead@': {
            templateUrl: 'app/products/show/subhead.tmpl.html'
          },
          'content@': {
            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
  });

为什么在两个实例中都会调用show controller?对于不同的状态使用相同的模板是否存在问题?

这里的ui-view片段:

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

1 个答案:

答案 0 :(得分:0)

找到我的问题的答案,以防其他人偶然发现类似的事情:网址被匹配两次。我没有将任何正则表达式应用于productId,因此/ create被解释为有效的产品ID。