Tôi mới đến angularjs. Tôi gặp sự cố khi truy cập giá trị được trả về từ dịch vụ angularjs trong bộ điều khiển. Tiếp theo là các mã trong bộ điều khiển:Làm thế nào để bộ điều khiển chờ dịch vụ trả về một giá trị và cách truy cập giá trị trả về trong bộ điều khiển?
'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
Và mã trong dịch vụ tìm kiếm là như sau:
'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.
Khi mã thực thi nó mang lại cho lỗi cho .Sau đó chức năng như Fallows :
LoạiError: undefined không phải là hàm và giá trị trong bộ điều khiển không được truy cập.
Vui lòng trợ giúp.
bạn phải thực hiện ' '$ q'' vào nhà máy, hãy kiểm tra này: https://docs.angularjs.org/api/ng/service/$q –
Hey Deblaton Jean-Philippe, Tôi gặp rắc rối khi triển khai $ q trong nhà máy của tôi. Bạn có thể hướng dẫn tôi qua quá trình ... !! ? –
$ firebase là gì và tại sao bạn trả lại lần đầu tiên? Đôi khi bạn có nhập vòng lặp "for" của mình không? –