简体   繁体   English

如何编写在AngularJS中返回true / false的服务?

[英]How do I write a service that return true/false in AngularJS?

I had been reading up on AngularJS Factory and Services and came across a lot of examples. 我一直在阅读AngularJS工厂和服务,并遇到了很多例子。 But I'm not understanding the logic behind them and can't get them to work either. 但是我不理解它们背后的逻辑,也无法使它们工作。

myApp.service('loginCheck', function(Facebook) {
this.loggedIn = false;
Facebook.login(function(response) {
    if (response.status == 'connected') {
        this.loggedIn = true;
    }
});
});

I need to get this service to return the loggedIn variable, if my idea of the concept is right. 如果我对概念的想法是正确的,那么我需要获得此服务以返回loggingIn变量。 Should I use a factory instead? 我应该改为使用工厂吗? I need to store this variable all along, without making the call again every once I need it. 我需要一直存储此变量,而不必在需要时再次进行调用。 How can I do it. 我该怎么做。 I couldn't really understand the difference between factory and service. 我无法真正理解工厂和服务之间的区别。 I'm a beginner here. 我是这里的初学者。

if(loginCheck.loggedIn){
    console.log("logged in!");
}
else{
    console.log("not logged in :/")
}

Thank you. 谢谢。

You can do this, 你可以这样做,

app.service('MyUser', [function($scope) {
  this.loggedIn = false;
  return {
    getStatus: function() {
       //call fb api
       this.loggedIn = true;
       return this.loggedIn;
    }
  }
}]);

DEMO 演示

 var app = angular.module('app', []); app.controller('loginController',['$scope', 'MyUser',function($scope, MyUser) { $scope.isloggedin = MyUser.getStatus(); alert($scope.isloggedin); }]); app.service('MyUser', [function($scope) { this.loggedIn = false; return { getStatus: function() { this.loggedIn = true; return this.loggedIn; } } }]); 
 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> </head> <body ng-app="app" ng-controller="loginController"> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.6/angular.js"></script> </body> </html> 

Define a factory and passed to your controller as dependency. 定义一个工厂,并将其作为依赖项传递给您的控制器。

var myApp = angular.module('myApp', []);
myApp.factory('loginCheck', ["Facebook", function(Facebook) {
  var object = {
    loggedIn: false,
    loginFacebook: function() {
      // do facebook login here:
      //Facebook.login(function(response) {
      //  if (response.status == 'connected') {
          object.loggedIn = true;
      //  };
      }
    };

  object.loginFacebook();
  return object;
}]);

myApp.controller('myCtrl', ['loginCheck', '$scope', function(loginCheck, $scope) {
  $scope.loggedIn = loginCheck.loggedIn;
}]);

check this jsFiddle sample here . 在此处检查此jsFiddle示例。

I don't know about facebook authentication, but I think these links may help you : https://blog.brunoscopelliti.com/facebook-authentication-in-your-angularjs-web-app/ , https://developers.facebook.com/docs/javascript/howto/angularjs 我不知道关于Facebook的身份验证,但我认为这些链接可以帮助你: https://blog.brunoscopelliti.com/facebook-authentication-in-your-angularjs-web-app/HTTPS://developers.facebook .com / docs / javascript / howto / angularjs

To find different between service and factory look at this question or this link . 要找到服务和工厂之间的区别,请查看此问题此链接

暂无
暂无

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

相关问题 如何为 Javascript 中的三元运算符返回真或假? - How do I return true or false for ternary operator in Javascript? 如何使用jQuery noty插件返回true或false? - How do I return true or false using jQuery noty plugin? 等待异步函数返回true或false - 如何检查返回值? - Waiting for async function to return true or false - how do I check return value? 将var t =!0 / var t =!1用作true / false有多安全? 他们是否总是返回true布尔值true / false? - How safe is using var t=!0 / var t=!1 for true / false? Do they always return true boolean true / false? removeAttribute不返回true或false - removeAttribute do not return true or false 从laravel返回true或false到Angularjs $ scope - Return true or false from laravel to Angularjs $scope AngularJS检查条件并返回true或false - AngularJS check condition and return true or false AngularJS:service的布尔方法是否应该返回为true / false或者被解析/拒绝的promise? - AngularJS : Should service's boolean method return promise that resolves to true/false, or that gets resolved/rejected? 如果给定的小数点四舍五入为整数,如何修改该函数以返回true;否则,返回false? - How do I modify a function to return true if the given decimal is even when it is rounded to an integer and false otherwise? 我如何检查数组编号从“pages”到“endingPages”,如果它们匹配返回true,否则为false - how do i check array number form "pages" to "endingPages" if they match return true else is false
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM