javascript - What is "=*" in AngularJS -
i've come across way of isolate binding
specification:
scope: { property: "=*" }
what asterisk mean here? can please provide example?
the isolate binding
=*
shallow watch change in collection.
let me explain bit:
normally watchcollection variables use in scripts below:
$scope.arr = ['foo', 'bar', 'lorem', 'ipsum']; $scope.arrcount = 4; $scope.$watchcollection('arr', function(new, old) { $scope.arrcount = new.length; });
but if want object binding in html attribute itself?
<my-directive my-attr="arr"><!--$scope.arr-->
and, if this:
scope: { myattr: "=*" }
now, directive attributes assigned should shallow watch. , use of watchcollection of use time.
so, =*
used when need shallow watch changes (i.e. $watchcollection instead of $watch) can use =* or =attr (=? or =*?attr if property optional) described in docs.
although, can use =
watchcollection watch object or array can use =*
same. difference using =*
method collection becomes true , when collection becomes true angularjs use $watchcollection remove watcher , uses $watch remove watcher when collection false:
if (definition.collection) { //using `=*` removewatch = scope.$watchcollection(attrs[attrname], parentvaluewatch); } else { //using `=` removewatch = scope.$watch($parse(attrs[attrname], parentvaluewatch), null, parentget.literal); }
so, when want shallow watch collection need use =*
.
Comments
Post a Comment