从工厂到控制器的角度传递数据 [英] Angular passing data from factory to controller

查看:128
本文介绍了从工厂到控制器的角度传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图存储一个授权的用户ID变量,我可以传递给控制器​​。我知道有一个问题,我试图从我的工厂对象的封闭内传递数据,但我坚持如何解决它。



这是我的工厂:

$ $ p $ myApp.factory('Authentication',function($ firebase,
$ firebaseAuth,FIREBASE_URL ,$ location){

var ref = new Firebase(FIREBASE_URL);
var simpleLogin = $ firebaseAuth(ref);

var authorized;

var myObject = {
login:function(){
return simpleLogin。$ authAnonymously()。then(function(authData){
authorized = authData.uid;
console.log(登录为:,authData.uid);
})。catch(function(error){
console.error(Authentication failed:,error);
});
},
auth:authorized
} // myObject

return myObject;
});

这里是我的控制器:

<$ p $ myApp.controller('MeetingsController',


函数($ scope,$ firebase,Authentication){

var ref =新Firebase('http://i2b2icons.firebaseio.com/');
var meetings = $ firebase(ref);

$ scope.authid = Authentication.auth;

$ scope.meetings = meetings。$ asObject();
// $ scope.id = = Authentication.login.id;
$ scope.addMeeting = function(){
meeting。$ push({
name:$ scope.meetingname,
date:Firebase.ServerValue.TIMESTAMP
})。then(function(){
$ scope。 (key); meetingname ='';
});
} // addmeeting

$ scope.deleteMeeting = function(key){
meetings。
} // deletemeeting

}); // MeetingsController

我真的只想获取$ scope.authid变量来获取从myObject的登录功能授权的值。


$ b 登录方法应该已经通过这个控制器登录了:

 <$ ($ scope,$ firebaseAuth,$ location,Authentication){


$ scope.login = function(){
Authentication.login();
} // login


}); // RegistrationController


解决方案

code> authorized 在您的工厂中,它与您试图在您的控制器中访问的 Authentication.auth 无关当然你在创造这个因素的时候给它设定了价值,而不是这个意图)。而是返回工厂中的预定义对象,并从中返回该对象。

$ $ p $ myApp.factory('Authentication',function($ firebase,
$ firebaseAuth,FIREBASE_URL,$ location){

var ref = new Firebase(FIREBASE_URL);
var simpleLogin = $ firebaseAuth(ref);
//预定义工厂$ b $ (){
login:login,
authorized:null
};

函数login(){
return simpleLogin。$ authAnonymously() .then(函数(authData){
factory.authorized = authData.uid; //设置属性
})。catch(function(error){});
}
//返回
返回工厂;
});

使用这个提供的参数,工厂和属性的更新将会反映出来在您的控制器中填充数据的方法)。另一种方法是在你的工厂中使用getter函数来返回auth对象,或者你也可以缓存login函数返回的promise,并在事件发生时将其返回并使其失效。


I'm trying to store an authorized user id variable, which I can pass to controllers. I know there's an issue with how I'm trying to pass the data from inside the closure of my factory object, but I'm stuck on how to fix it.

Here is my factory:

myApp.factory('Authentication', function($firebase, 
  $firebaseAuth, FIREBASE_URL, $location) {

  var ref = new Firebase(FIREBASE_URL);
  var simpleLogin = $firebaseAuth(ref);

  var authorized;

  var myObject = {
    login : function() {
    return simpleLogin.$authAnonymously().then(function(authData) {
    authorized = authData.uid;
  console.log("Logged in as:", authData.uid);
}).catch(function(error) {
  console.error("Authentication failed:", error);
});
    },
    auth : authorized
  } //myObject

  return myObject;
});

Here is my controller:

myApp.controller('MeetingsController', 


function($scope, $firebase, Authentication) {

  var ref = new Firebase('http://i2b2icons.firebaseio.com/');
  var meetings = $firebase(ref);

  $scope.authid = Authentication.auth;

  $scope.meetings = meetings.$asObject();
//  $scope.id = = Authentication.login.id;  
  $scope.addMeeting=function() {
    meetings.$push({
      name: $scope.meetingname,
      date: Firebase.ServerValue.TIMESTAMP
    }).then(function() {
      $scope.meetingname = '';
    });
  } //addmeeting

  $scope.deleteMeeting=function(key) {
    meetings.$remove(key);
  } //deletemeeting

}); //MeetingsController

I'm really just trying to get the $scope.authid variable to pick up the value of auuthorized from the the login function of myObject.

The login method should have been called already by logging in via this controller:

myApp.controller('RegistrationController', 


function($scope, $firebaseAuth, $location, Authentication) {


  $scope.login = function() {
    Authentication.login();
  } //login


}); //RegistrationController

解决方案

You are just setting the local variable authorized in your factory, it has no relation to the Authentication.auth that you are trying to access in your controller (unless ofcourse you set the value to it while creating the factor and which is not the intention anyway). Instead return a predefined object in your factory and return that object from it. Set the property on the object reference.

myApp.factory('Authentication', function($firebase, 
      $firebaseAuth, FIREBASE_URL, $location) {

    var ref = new Firebase(FIREBASE_URL);
    var simpleLogin = $firebaseAuth(ref);
    //Predefine the factory
    var factory = {
       login: login,
       authorized: null
    };

    function login() {
       return simpleLogin.$authAnonymously().then(function(authData) {
          factory.authorized = authData.uid; //Set the property here
      }).catch(function(error) {});
    } 
   //return it
   return factory;
});

With this provided you have the reference of the factory and updates to its property will reflect (provided you invoke the method that populates the data) in your controller. Another way would be to use a getter function in your factory to return the auth object, or you can as well cache the promise returned by login function and return it and invalidate it when an event occurs that log the user out.

这篇关于从工厂到控制器的角度传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆