具有一个元素的单独范围的多个指令

时间:2014-12-28 11:15:18

标签: javascript angularjs angularjs-directive angularjs-scope

one元素可以有多个带有各个范围的指令吗?

假设我们有自定义指令的子控制器的范围和任何指令(这里是“ng-class”):

<custom-directive data-model="model">
  <input class="child" data-ng-class="controllerScopeValue">
</custom-directive>

现在我们要为子项添加具有隔离范围的额外指令。像这样:

angular.module('core').directive('customDirective', [function($compile) {
  return {
    scope: {
      'model': '='
    },
    compile: function(templateElement, templateAttrs) {
      templateElement.children().attr('data-ng-model', 'directiveScopeValue');
      return function($scope) {
        $scope.directiveScopeValue = 'directive\'s scope value';
      }
    }
  };
}]);

那么,如何为每个指令保留各个范围?

1 个答案:

答案 0 :(得分:0)

不,这是不可能的,如果你尝试这样做,你会得到类似于

的错误

多个指令[myDirective1,myDirective2]要求新/隔离范围

plunker

忽略下面的虚拟代码

app.directive('myDirective1', ['$document',
function ($document) {
return {
    restrict: 'A',
    replace: false,
    scope : {},

app.directive('myDirective2', ['$document',
function ($document) {
return {
    restrict: 'A',
    replace: false,
    scope : {},