Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何使控制器等待服务返回值,以及如何访问控制器中的返回值?_Angularjs_Node.js_Angularjs Scope_Firebase_Firebasesimplelogin - Fatal编程技术网

Angularjs 如何使控制器等待服务返回值,以及如何访问控制器中的返回值?

Angularjs 如何使控制器等待服务返回值,以及如何访问控制器中的返回值?,angularjs,node.js,angularjs-scope,firebase,firebasesimplelogin,Angularjs,Node.js,Angularjs Scope,Firebase,Firebasesimplelogin,我不熟悉angularjs。我在访问angularjs服务在其控制器中返回的值时遇到问题。 以下是控制器中的代码: 'use strict'; app.controller('AdminTaskCtrl', function ($scope, Search) { $scope.buildEnquiry = function (collegeId, ) { Search.getIdByEmail(collegeId).then ( function ( result ) {

我不熟悉angularjs。我在访问angularjs服务在其控制器中返回的值时遇到问题。 以下是控制器中的代码:

'use strict';
app.controller('AdminTaskCtrl', function ($scope, Search) {
    $scope.buildEnquiry = function (collegeId, ) {
        Search.getIdByEmail(collegeId).then ( function ( result ) {
            $scope.uId = result;
            console.log($scope.uId);
        });
    };
});//controller ends here
'use strict';
           app.factory('Search',function ($firebase, FIREBASE_URL, $rootScope) {
                          var simpleuser = "";
                          getIdByEmail: function(counsellorEmail) {
                          var collegeuserArray = ($firebase(new Firebase(FIREBASE_URL+"abc/def/")).$asArray());
                          collegeuserArray.$loaded(function(collegeuserArray) {
                              for(var i=0; i<collegeuserArray.length; i++) 
                              {
                                if((collegeuserArray[i].$value) == counsellorEmail)
                                {
                                     simpleuser = collegeuserArray.$keyAt(collegeuserArray[i]);
                                     console.log(simpleuser);
                                     return simpleuser;
                                }
                              }
                          }, function(error) {
                              console.error("Error:", error);
                          });
                    }
                  };     
            );//service ends here.
搜索服务中的代码如下:

'use strict';
app.controller('AdminTaskCtrl', function ($scope, Search) {
    $scope.buildEnquiry = function (collegeId, ) {
        Search.getIdByEmail(collegeId).then ( function ( result ) {
            $scope.uId = result;
            console.log($scope.uId);
        });
    };
});//controller ends here
'use strict';
           app.factory('Search',function ($firebase, FIREBASE_URL, $rootScope) {
                          var simpleuser = "";
                          getIdByEmail: function(counsellorEmail) {
                          var collegeuserArray = ($firebase(new Firebase(FIREBASE_URL+"abc/def/")).$asArray());
                          collegeuserArray.$loaded(function(collegeuserArray) {
                              for(var i=0; i<collegeuserArray.length; i++) 
                              {
                                if((collegeuserArray[i].$value) == counsellorEmail)
                                {
                                     simpleuser = collegeuserArray.$keyAt(collegeuserArray[i]);
                                     console.log(simpleuser);
                                     return simpleuser;
                                }
                              }
                          }, function(error) {
                              console.error("Error:", error);
                          });
                    }
                  };     
            );//service ends here.
“严格使用”;
app.factory('Search',函数($firebase,firebase\u URL,$rootScope){
var simpleuser=“”;
GetIDByMail:函数(顾问邮件){
var collegeuserArray=($firebase(新的firebase(firebase_URL+“abc/def/”)))。$asArray();
collegeuserArray.$loaded(函数(collegeuserArray){

对于(var i=0;i直接回答您的问题,
$loaded
,因此这里最简单的答案是从您的服务中返回它,而不是麻烦使用$q和所有其他东西

这里的整个前提似乎是有缺陷的,这是一个错误。似乎有几个黑客试图颠覆这些LIB的预期用途,并且像一个好的、可靠的阅读和将在这里节省很多痛苦和不必要的复杂性

这里工厂的使用是一种颠覆,耦合性很强。语法无效,无法编译。最终,目标是向AngularFire返回的同步数组添加一个搜索方法,这应该使用$extendFactory完成

app.factory('firebaseRef', function(FIREBASE_URL) {
   return function(path) {
      var ref = new Firebase(FIREBASE_URL);
      if( path ) { ref = ref.child(path); }
      return ref;
   }
});

app.factory('SearchableArray', function($firebase, $FirebaseArray) {
   var ArrayWithSearch = $FirebaseArray.$extendFactory({
     searchByEmail: function(emailAddress) {
        var res = null;
        for(var i=0, len=this.$list.length; i < len; i++ ) {
           if( this.$list[i].email === emailAddress ) {
              res = this.$list[i];
              break;
           }
        }
        return res;
     }
   });

   return function(ref) {
      return $firebase(ref, {arrayFactory: ArrayWithSearch}).$asArray();      
   }
});

app.controller('Controller', function($scope, SearchableArray, firebaseRef) {
   $scope.data = SearchableArray( firebaseRef('abc/def') );

   $scope.search = function(email) {
      console.log( $scope.data.searchByEmail(email) );
   };
});
app.factory('firebaseRef',函数(FIREBASE\u URL){
返回函数(路径){
var ref=新的Firebase(Firebase\u URL);
如果(路径){ref=ref.child(路径);}
返回ref;
}
});
应用程序工厂('SearchableArray',函数($firebase,$FirebaseArray){
var ArrayWithSearch=$FirebaseArray.$extendFactory({
searchByEmail:功能(电子邮件地址){
var-res=null;
对于(变量i=0,len=this.$list.length;i
您必须在工厂实施
$q
,请检查:嘿,Deblaton Jean-Philippe,我在工厂实施$q时遇到了问题。请您指导我完成这个过程…!!?什么是$firebase,为什么您先退回它?您有时会输入“for”吗loop?实际上firebase是我的nosql数据库,其中包含我需要的特定值集。我从firebase获取值,但问题是我无法在控制器中返回值。[.then]in controller给出了我在问题中提到的错误。是的,我为从firebase获得的每个值输入循环。执行此命令:
var something=Search.getIdByEmail(collegeId)
并告诉我
something
。顺便说一句,我不明白为什么代码在
返回($firebase(new firebase>)后继续(FIREBASE_URL+“abc/def/”);$asArray();
谢谢Deblaton Jean-Philippe。代码对我有用。这里的语法看起来也无效,是从原始问题复制的。另外,$loaded已经返回了一个承诺。