javascript - Angular how to set a model inside a factory service for use in a controller? -


is possible (and idea) set model (scope vars) in factory service?

for example, have factory defines model below how can init in controller?

(function () {     'use strict';     angular.module('services.auth', [])          .factory('authorisationservice', function ($rootscope, $http) {              // public variables             var authservice = {                 user: {                     email: '',                     password: ''                 }              };              return authservice;          });   })(); 

controller:

.controller('loginctrl', ['$scope', 'authorisationservice', function ($scope, authorisationservice) {       authorisationservice();                };           }]); 

sure can way, love use service , keep controller clean.

take @ demo:

var app = angular.module('app', []);    app.controller('mainctrl', function($scope, authorisationservice) {    $scope.authservice = authorisationservice.authservice;  });    app.factory('authorisationservice', function($rootscope, $http) {    var authorisationservice = {};      // public variables    var _authservice = {      user: {        email: 'test@gmail.com',        password: 'test123'      }    };      authorisationservice.authservice = _authservice;    return authorisationservice;  });
<!doctype html>  <html ng-app="app">    <head>    <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.8/angular.js" data-semver="1.4.8"></script>    <script src="app.js"></script>  </head>    <body ng-controller="mainctrl">    <p>email: {{authservice.user.email}}</p>    <p>password: {{authservice.user.password}}</p>  </body>    </html>


if have more questions let me know!


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -