Is angular region binding and (ampersand) one-time binding?

Is angular region binding and (ampersand) one-time binding? I see that this is called one-way binding, but is it also one-time?

Let's say I have:

<my-custom-directive data-item="item" />

And my directive is declared as follows:

.directive('myCustomDirective', [
'$log', function ($log) {
return {
    restrict: 'E',
    templateUrl: '/template.html',
    scope: {
        dataItem: '&'
    }
    controller: function ($scope) {
        // ....
    }
}])

The reason I ask if the snap is a one-time binding is because it looks like what I'm observing. If itemthe parent area is updated, then the directive is not updated.

Am I saying correctly that binding is one time?

To achieve what I want, where the directive stores a copy without affecting the element of the parent object - I did this:

.directive('myCustomDirective', [
'$log', function ($log) {
return {
    restrict: 'E',
    templateUrl: '/template.html',
    scope: {
        dataItemOriginal: '='
    },
    link: function ($scope) {
        $scope.$watch('dataItemOriginal', function () {
            $scope.dataItem = window.angular.copy($scope.dataItemOriginal);
        });
    },
    controller: function ($scope) {
   //....
   }
}])

Is this right or is there a better way?

+4
1

. - "=", $watch, . $ .

, :

.directive('myCustomDirective', [
'$log', function ($log) {
return {
    restrict: 'E',
    templateUrl: '<div ng-click="clicked()">Click me for current value</div>',
    scope: {
        item: '&'
    },
    controller: function($scope) {
        $scope.clicked = function(){
            alert(item());  //item() returns current value of parent $scope.item property
        }
        $scope.val = item();  //val is the initial value of $parent.item
        $scope.val = 42; //$parent.item is unaffected. 
    }

}])

& . , :

, . . angular.

, :

<my-custom-directive data-item="item"></my-custom-directive>

scope: {
    item: '&'
}

$scope.item , . , "item" . - .

, "" "", , & $parent.item, , "=", , $watch , . " ", .

, angular , $parse, "locals", . , , :

item({item: 42})

42, . .

+10

All Articles