【问题标题】:Controller for views in a state状态视图的控制器
【发布时间】:2014-10-18 10:57:17
【问题描述】:

我正在尝试在抽象状态中定义一个控制器,以便我可以为所有视图使用一个控制器,但如果控制器是在外部定义的,则不会调用控制器内部的函数。

.state('update', {
        url : '/update',
        abstract: true,
        template: '<ui-view/>',
        controller : function($scope) {   //works
            $scope.hello = function() {
                alert("hello");
            }
        }
        controller : 'updateController'  // Doesn't work
    })
    .state('update.detail', {
        url : '/view/:id',
        views : {
            "" : {
                templateUrl : 'update-detail.html'
            },

            "header@update.detail" : {
                templateUrl : 'header.html'
            },
            "genericnavigation@update.detail" : {
                templateUrl : 'generic-navigation.html'
            },
            "mnavigation@update.detail" : {
                templateUrl : 'mobile-navigation.html'
            },
            "updatecontent@update.detail" : {
                templateUrl : 'update-content.html'
            }
        }
    })

HTML

header.html
 <div ng-click="hello();"></div>  //Click event doesn't get fired for ( controller : 'updateController' )


app.controller('updateController', ['$scope', '$state', function($state, $scope) {
 console.log("inside update")
 $scope.hello = function() {
        alert("hello");
 }
}]);

【问题讨论】:

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


    【解决方案1】:

    看看你是如何定义你的控制器的:

    ['$scope', '$state', function($state, $scope)
    

    参数的顺序不正确。所以 $state 变量其实就是作用域,$scope 变量其实就是状态。

    【讨论】:

      猜你喜欢
      • 2010-09-25
      • 1970-01-01
      • 2020-04-10
      • 1970-01-01
      • 2014-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-25
      相关资源
      最近更新 更多