2015-11-08 58 views
1

我想通過一個函數在控制器屬性中添加一個值。 做這樣的事情:stateProvider - 控制器屬性

.state('integrator.optionCommandTabContent', { 
    url: '/integrator/optionCommandTabContent/:optionId/:ctrlName', 
    views: { 
     '[email protected]': { 
      templateUrl: '../../../angular-app/components/integrator/integrator-conf/option-conf/commands-tab/command-tab.html', 
      controller: function ($stateParams){        
       return $stateParams.ctrlName.toString();       
      }, 
      controllerAs: 'ctrl', 
      resolve: { 
       optionId: function($stateParams){ 
        return $stateParams.optionId; 
       } 
      } 
     } 
    } 
}); 

好像控制器屬性沒有得到函數的返回值。是否有可能做這樣的事情,但以另一種方式?

回答

0

A controller不應該返回任何東西。它應該提供操作並僅執行一些初始化和銷燬​​操作。例如。

controller: function ($stateParams, $scope, someService){  
    // naive sync init 
    this.items = someService.load($sateParams.id); 
    // example of action 
    this.edit = function(item) { 
     ... 
    } 
    // destroy on view leave 
    $scope.$on('$on', function(){ 
     ... 
    }); 
}, 

,並可能INT視圖消耗

<li ng-repeat="item in ctrl.items"> ... 
<button ng-click="ctrl.edit(item) ...