javascript - How do I change a scope variable with a service? -


I have an app, and there is an entry and login form inside the app.

The signup form, when the user creates the account, they are automatically logged in.

With the login form, the user can also login (obviously).

I have two controllers: one for the signup form, and one for the entry form. I want them to be able to share a service, 'login', because the user is getting login with both forms.

It is still working. My only issue is that the entry form sees the user's login credentials asynchronously against the database, and if this request is made on this script then it comes back, then it says that there was no match, then somebody Displays the model of the error message, which is shown when the 'login' service is not used as a service for a user, but it is not only for both If it is copied to the devices, then it works perfectly in 'SignupCtrl' it looks like:

  .controller ('SignupCtrl', ['$ scope', '$ http' Function ($ radius, $ http) {$ scope.error = incorrect; $ scope.logIn = function () {var data = // user's login credentials $ http ({// send credentials to server, ask if a mail Is}} .vivet (data) {if (data match === false) {$ scope.error = true;} and {// start session, redirect to new page, etc}})}}} ]);   

Then, inside the template:

  & lt; div ng-show = "error" & gt; Oh, is that so! That email / password does not exist & Lt; / Div & gt;   

This works fine; But as I said, not because the 'login' function is used as a service, the reason is that how can I update $ scope.error inside the service I'm unable to find out.

I have tried $ rootScope (which did not work):

 . Service ('login', [/ * injection * /, '$ rootscope', function (/ * injection * /, $ rootsecope) {return function email, password) {// All data is first as a code (if .match === Incorrect) {$ rootScope.error = true; // it does nothing)}}]); // In the Controller (service is injected as 'Login') $ scope.logIn = logIn;   

Then, in the template:

  & lt; Button ng-click = "login (user.email, user.password)" & gt; Login & lt; / Button & gt;   

The user can log in properly with the service The problem is just updating the $ scope.error variable with the service.

Perhaps if I can do it somehow, this will work:

    

Then in service:

  return function (email, password, scope) {// etc. If (data.match === incorrect) {Scope.error = true; }}   

Any thoughts? Thank you.

Smaller edits

Just to clarify, what I understand about services , they take place that normally is a global function, or Example:

  (function () {function globalFunction () {// etc} $ ('.ting') to avoid just repeating itself to perform an aggregate task inside the module. Click (Global function); $ ('. Otherthong'). Click (global function);} ());   

This could be a better example, but I think this idea is clear.

Should this concept be used within the angular services? If not, what I'm trying to do without service, is there a better way for me?

You can pass through a service in $ rootScope , but it Usually, it is not a good idea to corrupt your $ rootScope , though your problem with this issue is that the issue is not that a digest is forced and thus your controller $ scope change. If you wrap your $ rootScope.error inside a $ rootScope, post it in the code. $ Applied () things will work fine but the callback is very clean.

I recommend that you pass a callback in the service method and set your radius variable inside the callback:

  login: function (email, password, callback) { $ Http ({// send credentials to the server, ask if there is a match}). Feature (data) {callback (data.match);}}}   

In the controller:

  $ scope.logIn = function () {loginServcie.login ($ scope.email, $ scope.password, function (dataIsMatched) {// Now you have the result of the call Is, and dataIsMetched right or wrong $ scope.error =! Itaatmed;})}    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -