2013-12-12 50 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