2013-12-12 93 views
0

當創建與一個指令隔離範圍,但在指令中沒有模板,但與指令裏面的一些DOM,該指令裏面的DOM無法綁定到該指令的範圍。角1.2指令範圍問題

<div ng-controller="testCtrl"> 
    {{hehe}} 
    <hr/> 
    <div test-directive="hello" > 
     Directive Data: 
     <div>{{test}}</div> 
    </div> 
    </div> 

angular.module('app',[]) 
.controller("testCtrl",['$scope', function ($scope) { 

    $scope.hehe = "test from controller"; 

}]) 
.directive("testDirective",function(){ 
    return{ 
    scope: { 
     "testDirective": "=" 
    }, 
    controller: ['$scope', function ($scope) { 

     $scope.test = "test from directive"; 

    }] 
    }; 
}); 

Demo

在演示中,有兩個角的lib版本1.1.5和1.2.4,以及它們中的一個評論。

該代碼適用於1.1.5,但不適用於1.2.4。

有人可以解釋發生了什麼?

+0

感謝Fabrício幫我編輯問題。讓這個問題更清楚。 – areschen

回答

1

這是1.2.x中的更改。隔離範圍是真正的隔離,您只能通過您的指令(通過template:templateUrl:定義)的裏面的模板真正綁定它。

HTML中的模板永遠不會繼承指令的範圍。

隔離範圍的要點是將您的指令的內部實現與外部模板隔離。舊的行爲不會完全隔離該指令並使事情更加耦合。

除非您在指令中使用內部模板,否則不建議使用隔離範圍。當不使用template:templateUrl:時,您應該只使用scope: true或根本沒有使用範圍。

+0

我認爲我們之前有過不好的做法,非常感謝。 – areschen