angularjs - Questions about Angular $q defer object -


I'm new to kangaroo and I'm playing with it for about two weeks.

I had an implementation problem that I was able to solve. An overview of the problem is that I had a main.html that displayed all user information but the service function was not updated by this.list Try adding new information using the service this.addNewUser below is the working version of the code and version that does not work.

For this reason I have posted here, though I am resolving my problem is that I am eager to know why why . I want to understand that what is the difference between the two examples of difference object deferred does not work, but the first example is working perfectly well.

The difference is to do something with the object object, where I can declare it inside the function? Maybe I can not declare it globally?

I hope someone can tell why the first example works, but not the second example.


# 1 example

  // Add a single user this.addNewUser = function (username, email) {** var Deferred_2 = $ q.defer (); // Current active object asyn ajax call ** $ http ({method: 'GET', url: '/app/add.php', parameters: {"name": username, "email": email,}}} ) .viv (function (data, position, header, config) {deferred_2.resolve ('successful');}). Panic (function (data, position, header, config) {console.log ('data ::' data); console.log ('condition ::' status); console.log ('config ::' config) ;}); Withdraw deferred_2.promise; // Promise object result is}   

# 2 example

  var deferred_2 = $ q.defer (); // asyn ajax call for current active object / a single user this.addNewUser = function (username, e-mail) {$ http ({method: 'GET', url: '/app/add.php', Parameters : {"Name": username, "email": email,}} .success (function (data, position, header, config) {deferred_2.resolve ('successful');}) .error (function (data, Position, header, config) console.log ('data ::' + data); console.log ('position ::' status); console.log ('config ::' + config);}); return deferred_2 .promise; // Promise object is the result}   

The problem I am facing is That is, when I call any other function that is shown below, then it is not updating main.html new data which is now available from this.addNewUser .

  // List all users to list = list (function) {console .log ('About to begin'); // Get data $ Http ({method: 'GET', url: '/app/get.php',}) .success (function (data, position, header, config) {deferred_2.resolve (data);}) .error ( Function (Data, Status, Header, Config) {Deferred_2.reject ("Something bad is here");}); Withdraw deferred_2.promise; }    

Services in AngularJS are singleton, meaning that the global deferred_2 Your first function will already be sorted (or rejected) by

to: $ q

The object of the promise object is to allow interested parties to get access to the result of deferred work when it is completed.

Because the deferred work has already been completed, you will not have any callback to trigger calls to resolve or reject the call later, It does not appear that there is no reason to share the adjournment between functions - if you want to update the list while adding a new user, then I suggest that you use $ emit , < See code> $ $ da and $ on for methods on yahoo for events on the event)

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -