我想创建一个同时具有scope参数和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">现在这个错误是非常有用的,我得到它的角度并不像我希望的那样工作。
我可以从指令中删除"scope:{..}“参数,并在控制器中定义它,但这不是我需要的。
我需要的基本上是为自定义指令提供ng-controller,然后为该控制器提供额外的作用域变量。
我如何才能做到这一点?
发布于 2016-07-29 18:19:40
你必须用标签包装你的指令,并把控制器放在那里。
<div ng-controller="RegisteredController">
<csm-dir name="scopeParam">
<!--Here I want to have content-->
{{name}}
</csm-dir>
</div>没有办法在一个标记中获得两个独立的作用域。
https://stackoverflow.com/questions/38656193
复制相似问题