Click here to Skip to main content
16,022,901 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,
I have a case but i really don't know why . Pls explain for me:
In view:
<button ng-click="additem()">Add Item/button>
< div id="employeesPopup" ng-repeat="e in employeesReceiveMessage" >
< div class="col-md-2">
{{e.Name}}
< /div>

In js file:
With code :
$scope.additem = function () {
$scope.employeesReceiveMessage = [{
ID: 1,
Name: 'A B'
}];
};
It works correctly. Div employeesPopup display item which is assigned.

With code in select event in autocomplete:
$("#employee").autocomplete({
select: function (event, ui) {
$scope.employeesReceiveMessage = [{
ID: 1,
Name: 'A B'
}];
}
})
It does not update the view. I have to put $scope.$apply(); after to work correctly.

Pls explain for me.
Thanks.
Posted

1 solution

Hi Alixeer,

Your code deals with two worlds here. AngularJS world and jQuery world (or non-AngularJS world).

ng-click wraps the $apply() call internally. So you don't have to call it explicitly. As a result $apply() will be called automatically. After $apply() ends, digest cycle ($rootScope.$digest()) runs and watchers will be called. This updates the DOM with the new model value (as you have {{}}).

But the following model update from the jQuery doesn't trigger this flow.

JavaScript
<pre lang="php">$("#employee").autocomplete({
select: function (event, ui) {
$scope.employeesReceiveMessage = [{
ID: 1,
Name: 'A B'
}];
}
})


As a result, you have to explicitly call $apply() to trigger the above mentioned flow.

Hope that makes sense.

Regards,
Roshan.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900