简体   繁体   English

如何在控制器中使用角度服务

[英]How to use angular services with controllers

I'm new to angular and I've been told that it's better to make services do the heavy lifting for the controllers, but I'm finding it diffcult to make use of the services I've created. 我对angular并不陌生,有人告诉我,最好让服务来承担控制器的重担,但是我发现很难利用我创建的服务。 I've seen several questions on this but I can't find solutions. 我已经看到了几个问题,但是找不到解决方案。

Here's my service 这是我的服务

(function () {
  'use strict';

var office = angular.module('Office', []);

office.factory('auth', ['$http', '$localForage', '$scope', function ($http, $localForage, $scope) {

    var auth = {}

    auth.login = function (credentials) {
        $http.post('/auth_api/login', credentials)
            .then(function (data) {
                    $localForage.setItem('user', data.data)
                },
                function () {
                    $scope.login_error = 'Invalid Username/password'
                })
    }

    $localForage.getItem('user').then(function (data) {
        auth.isAuthenticated = !!data.id
    })

    return auth
}])

And here's my controller 这是我的控制器

office.controller('LoginController', ['$scope', 'auth', function ($scope, auth) {

    $scope.login = auth.login($scope.user)
}])

I have created a simpler version from your code.. and its working here . 我已经根据您的代码创建了一个更简单的版本。 check the link - fiddle 检查链接- 小提琴

app.factory('auth', ['$http', function ($http) {
    var auth = {};
    auth.login = function (credentials) {
      return "success";
    }
    return auth;
}]);

replace login function with your implemenation 用您的实现替换登录功能

Your code is correct but as you are not returning anything from the "auth" factory, you are not getting any update in the controller. 您的代码是正确的,但是由于您没有从“ auth”工厂返回任何内容,因此控制器中没有任何更新。 Change your code as shown below to return the data from factory or any message acknowledging the login. 如下所示更改代码,以从工厂返回数据或任何确认登录的消息。

Factory : 工厂:

 (function () {
  'use strict';

   var office = angular.module('Office', []);

   office.factory('auth', ['$http', '$localForage', '$scope', function ($http, $localForage, $scope) {

   var auth = {}

   auth.login = function (credentials) {
    return $http.post('/auth_api/login', credentials)
        .then(function (data) {
                $localForage.setItem('user', data.data);
                setAuthentication(true);
                return data.data; 
            },
            function (err) {
                return err;
            });
   }

   auth.setAuthentication = function (isLoggedIn){
     this.isAuthenticated = isLoggedIn;
   }

   return auth;
 }]);

Controller : 控制器:

office.controller('LoginController', ['$scope', 'auth', function ($scope, auth) {
      $scope.login = function (){
       auth.login($scope.user).then(function (data){
         $scope.userDetails = data;
       }, function (err){
         $scope.loginError = 'Invalid Username/password';
       });
      }
}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM