【发布时间】:2014-10-16 17:19:58
【问题描述】:
我有一个模板(用于模式弹出窗口),根据它的功能,应该加载不同的控制器:
<div id="MsgBoxBack" ng-controller="{{notification.ctrl}}">
控制器:
app.controller('MainCtrl', ['$scope', function($scope){
$scope.notification = {
ctrl: 'logout',
//...
};
}]);
app.controller('logout', ['$scope', function($scope){
//...
}]);
当我尝试通过范围变量设置控制器名称时,我收到以下错误:
Error: [ng:areq] Argument 'notification.ctrl' is not a function, got string
那么我应该如何根据作用域变量设置控制器呢?
我有一个关于使用范围变量设置 ng-click 函数的相关问题,这个例子与 here 相同。
更新
Girafa 给了我一个想法,让我以不同的方式解决这个问题。 我没有更改控制器,而是选择使用一个带有基于范围变量的 switch 语句的通用控制器:
<div id="MsgBoxBack" ng-controller="notification">
控制器:
app.controller('MainCtrl', ['$scope', function($scope){
$scope.notification = {
ctrl: 'logout',
//...
};
}]);
app.controller('notification', ['$scope', function($scope){
switch($scope.notification.ctrl) {
case 'logout':
console.log('logout');
//...
break;
}
}]);
这并没有解决最初的问题,但似乎是一个简单的解决方法。如果有人知道更好的方法,请告诉我,我很乐意更新问题或接受任何更好的答案。
【问题讨论】:
标签: javascript angularjs angularjs-scope