0

When creating a directive with isolate scope, but no template in the directive, but with some dom inside the directive, the dom inside the directive can not bind to the scope of the directive.

  <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

In the demo, there are two angular lib versions, 1.1.5 and 1.2.4, and one of them is commented.

The code works with 1.1.5 but not with 1.2.4.

Can someone explain what is happening?

1
  • Thanks Fabrício for help me edit the question. make this question more clear. Commented Dec 13, 2013 at 2:41

1 Answer 1

1

This is a change in 1.2.x. The isolate scope is truly isolate and you can only really bind to it from a template inside your directive (defined through template: or templateUrl:).

The template in your HTML will never inherit the scope of your directive.

The point of isolate scopes is to isolate the internal implementation of your directive from the outer template. The old behavior dind't fully isolate the directive and made things more coupled.

It is not recommended to use isolate scope unless you are using an internal template in the directive. When not using template: or templateUrl: you should just use scope: true or just no scope at all.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.