【问题标题】:stateProvider - controller propertystateProvider - 控制器属性
【发布时间】:2015-11-08 08:12:17
【问题描述】:

我试图通过一个函数在控制器属性中输入一个值。 做这样的事情:

.state('integrator.optionCommandTabContent', {
    url: '/integrator/optionCommandTabContent/:optionId/:ctrlName',
    views: {
        'optionConfigTabContent@': {
            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;
                }
            }
        }
    }
});

控制器属性似乎没有得到函数的返回值。是否有可能以另一种方式做这样的事情?

【问题讨论】:

    标签: angularjs angularjs-directive angular-ui-router


    【解决方案1】:

    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(){
            ...
         });
    },
    

    这可以在你的视野中消费

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

    【讨论】:

      猜你喜欢
      • 2016-11-11
      • 2015-04-05
      • 1970-01-01
      • 2014-12-28
      • 2016-07-18
      • 2014-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多