【发布时间】:2016-07-29 10:12:02
【问题描述】:
我想创建同时具有作用域参数和 ng-controler 的指令。
这是该指令的外观:
<csm-dir name="scopeParam" ng-controller="RegisteredController">
<!--Here I want to have content-->
{{name}}
</csm-dir>
我在我的应用程序某处声明了“RegistertedController”。
angular.module('app')
.controller('RegisteredController', ['$scope', function ($scope) {
//$scope.name should be accessible here
}]);
我正在尝试声明指令:
angular.module('app')
.directive('csmDir', [function () {
return {
restrict: 'AE',
replace: true,
transclude: true,
scope: {
name: '='
},
template: '<section class="layout"><ng-transclude></ng-transclude></section>',
link: function (scope, element, attr, ctrl, transclude) {
element.find('ng-transclude').replaceWith(transclude());
//here I also need scope.name parameter
}
};
});
我得到错误:
Multiple directives [ngController, csmDir] asking for new/isolated scope on: <csm-dir name="scopeParam" ng-controller="RegisteredController">
现在这个错误信息量很大,我知道它的角度不能像我想的那样工作。
我可以从指令中删除“范围:{..}”参数并在控制器中定义它,但这不是我需要的。
我需要的基本上是为自定义指令提供 ng-controller,然后为该控制器提供额外的范围变量。
我怎样才能做到这一点?
【问题讨论】:
-
AngularJS 组件不够用吗?
标签: javascript angularjs angular-directive ng-controller