簡體   English   中英

AngularJS:指令對模板和控制器/鏈接使用單獨的作用域

[英]AngularJS: directive uses separate scopes for template and controller/link

我正在嘗試使用指令進行模板化,但不幸的是,每個指令似乎都為模板和控制器/鏈接功能設置了單獨的作用域。

plnkr的示例:

<body ng-app="App">
    <h2>Directive with Isolating Scope</h2>
    <isolating some-value="isolated">{{someValue}}</isolating>

    <h2>Directive with Shared Scope</h2>
    <sharing some-value="shared">{{someValue}}</sharing>
</body>
var app = angular.module('App', []);

app.directive('isolating', function(){
  return {
    'restrict': 'E',
    'scope': {
      'someValue': '@'
    },
    'transclude': true,
    'template': '<div ng-transclude></div>',
    'link': function(scope, element, attrs){
      scope.someValue = attrs.someValue;
    }
  };
});

app.directive('sharing', function(){
  return {
    'restrict': 'E',
    'transclude': true,
    'template': '<div ng-transclude></div>',
    'link': function(scope, element, attrs){
      scope.someValue = attrs.someValue;
    }
  };
});

我在Batarang看到的內容:(括號中的指令名稱)

< Scope (002)
    < Scope (003)    <= (isolating) contains the isolated scope
    < Scope (004)    <= (isolating) contains the template scope
    < Scope (005)    <= (sharing)   contains the shared scope

如何為模板使用隔離范圍003? 范圍004似乎完全沒有必要。

AngularJS版本是1.2.0-rc3。

只需從兩者中刪除模板屬性。

編輯:使用此特定模板,您應該注意,包含會創建從父級繼承的隔離范圍的同級作用域。 文本將使用已包含范圍,因此它不了解隔離范圍的值。

如果在DOM中將{{someValue}}切換為{{isolated}} ,您會發現我在想什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM