Isolated Scale View Snap

I have been working with isolated scope directives for a bit of time, and a question arises, observing its behavior:

Why can't I bind the variables that I define inside the directive, the inherited scope, directly to the view?

Let me show an example of this code pen: http://codepen.io/anon/pen/VLKjrv

When I create a new $ scope variable inside the directive controller and I try to associate it with a view, it does not work. On the other hand, when I bind this variable to html, which comes from the template directive attribute, it works.

Check the code:

<body ng-app="isolated-test-app">
<section ng-controller="isolatedTestCtrl">
    <article>
      <h1>test1</h1>
        <div isolated-directive binding-from-attr="test">
            <span ng-bind="test"></span>
            <span ng-bind="test2"></span>
        </div>
      <h1>test2</h1>
        <div isolated-directive-number-two binding-from-attr="test">
        </div>

    </article>
</section>

angular.module('isolated-test-app', [])

.controller('isolatedTestCtrl', function isolatedTestCtrl($scope){
    $scope.test = 'Binded from parent controller';
})

.directive('isolatedDirective', function isolatedDirective(){

    var directive = {
        scope: {
          bindingFromAttr: '=',
        },
        controller: function directiveController($scope){
           $scope.test2 = 'Binded from directive controller!';
        },
    };

    return directive;
})

.directive('isolatedDirectiveNumberTwo', function isolatedDirective2(){

    var directive = {
        scope: {
          bindingFromAttr: '=',
        },
        template:'<span ng-bind="bindingFromAttr"></span>\
                  <span ng-bind="test2"></span>',
        controller: function directiveController($scope){
           $scope.test2 = 'Binded from directive controller!';
        },
    };

    return directive;
})

test1

Associated with the parent controller

test2

Associated with the parent controller

Connected to a directory controller!

I was expecting the result of test2 on test1.

Why is this happening?

+4
3

.

(scope: {}) , . , . , , . "" (transclude: true) (, , ).

, $scope.$id, , :

<div>parent scope: {{$id}} (will be 2)</div>

<isolate-scope>
   contents of isolate scope directive: {{$id}} (will also be 2)
</isolate-scope>

<isolate-scope-with-template>
   contents will be replaced with the template
</isolate-scope-with-template>
.directive("isolateScopeWithTemplate", function(){
   return {
     scope: {},
     template: "template: {{$id}} (will NOT be 2)"
   }
})

(, $id )

(scope: true) , , , ( - , , ).

, :

<span ng-bind="test2"></span> $scope.test2 .

<span ng-bind="test2"></span> isolatedDirectiveNumberTwo , $scope.test2 = 'Binded from directive controller!'.

+6

http://codepen.io/anon/pen/MwjjBw

1 test/test2, dom . , ,

$scope.$parent.test2 = "" ; 

2, , , dom- , ( , $compile / ).

, test1 , .

0

, New Dev

isolate scope (scope: {}) , . , .

So, I found out that there is a difference between the contents and the template of the directive and how the region is inherited on isolated objects. For my application setting the scope to true, my problem has been completely resolved.

In addition, the kwan245 solution is a real good job around this problem.

Both answers cleared my mind, thanks a lot to New Dev and kwan245 :)

0
source

Source: https://habr.com/ru/post/1589094/


All Articles