Gwon  Boram
Gwon Boram

Reputation: 41

angularjs directive variable reference

I really don't no this angularjs variable working.

look at the my code -> http://jsfiddle.net/a9etkyz7/1/

$scope.ref and $scope.ref2 is not working!

$scope.ref= $scope.a

this is not reference?

this is copy?

please tell me answer

Upvotes: 2

Views: 1451

Answers (1)

ryeballar
ryeballar

Reputation: 30118

Only objects can be passed as reference, since $scope.a is assigned a primitive value, then changes to the assigned values in $scope.ref and $scope.ref2 does not reflect to the changes of $scope.a.

Tol solve this, you can change $scope.a into an object that holds a reference to the value you wish to change, and assign it to both $scope.ref1 and $scope.ref2.

DEMO

JAVASCRIPT

Controller

//....
$scope.a = {value: 12};
$scope.ref = $scope.a;
$scope.ref2 = {copyVale2: $scope.a};

Directive

//....
scope.a.value = newVal;

HTML

<test value="value" a="a">
    <div>ref a : {{ref.value}}</div>
    <div>ref2 a : {{ref2.copyVale2.value}}</div>
    <div>a : {{a.value}}</div>
</test>

Upvotes: 1

Related Questions