AngularJS's safety apply

Source: Internet
Author: User

$scope. safeapply = function (fn) {var phase = this. $root. $ $phase;        if (phase = = ' $apply ' | | phase = = ' $digest ') {if (FN && (typeof (fn) = = = ' function ')) {fn ();    }} else {this. $apply (FN); }};

When using Ng, you will encounter the situation of dynamically adding data, it is possible after you add data dynamically after the page because the rendering has been completed so that the newly added data can not be displayed on the page

If using the Select tag:

<select id= "Uidselect" class= "sel" ng-model= "Sid" Ng-init= "" ng-options= "user.name for user in users" > <option V Alue= "" Default>-select one-</option></select>

Since the data of the users array at the beginning is fixed, then if we want to add data to users after that, then the drop-down option will not change, and the various strategies of Niang will tell you to add $scope. $apply () on it. ... However, it is possible to add the following error message [$rootScope: InProg] $apply already in progress, so it is only necessary to use the method provided above,

First the top safeapply method into your scope, and then dynamically add the data after the use of $scope. safeapply (); You can have it ....


AngularJS's safety apply

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.