admin管理员组文章数量:1391818
Below is my Controller Code:
///////
//Diary Notes Controller
///////
diary.controller('NotesController', ['$scope','$http', 'notesService', function ($scope, $http, notesService){
//
//Get the Notes data back from the Server
//
$scope.updateFunc = function () {
var notes = notesService.getText();
console.log(notes);
};
$scope.updateFunc();
And my Services Code:
diary.factory('notesService', function ($http) {
return {
getText: function () {
$http.get('www/getNotes.php')
.then(
function (payload){
return payload.data;
});
}
}
});
Basically When I do console.log the controller returns undefined for the notes
variable which seems awkward as when i was using the controller to get the payload it works but returning the payload from the services doesn't seems to work.
Below is my Controller Code:
///////
//Diary Notes Controller
///////
diary.controller('NotesController', ['$scope','$http', 'notesService', function ($scope, $http, notesService){
//
//Get the Notes data back from the Server
//
$scope.updateFunc = function () {
var notes = notesService.getText();
console.log(notes);
};
$scope.updateFunc();
And my Services Code:
diary.factory('notesService', function ($http) {
return {
getText: function () {
$http.get('www/getNotes.php')
.then(
function (payload){
return payload.data;
});
}
}
});
Basically When I do console.log the controller returns undefined for the notes
variable which seems awkward as when i was using the controller to get the payload it works but returning the payload from the services doesn't seems to work.
4 Answers
Reset to default 2$http.get is asynchronous function, that returns HttpPromise
. There are couple ways how you can get data
1.Pass callback
, like this
diary.factory('notesService', function($http) {
return {
getText: function (callback) {
$http.get('www/getNotes.php')
.then(
function(payload) {
callback(payload.data);
});
}
}
});
notesService.getText(function (notes) {
console.log(notes);
});
2.return promise
diary.factory('notesService', function($http) {
return {
getText: function () {
return $http.get('www/getNotes.php');
}
}
});
notesService.getText().then(
function(payload) {
callback(payload.data);
});
You get undefined
because you're not returning anything from getText()
. Add a return statement before the $http
call in your method:
getText: function () {
return $http.get('www/getNotes.php')
.then(function (payload) {
return payload.data;
});
}
Afterwards call the then
method of the promise to get the value:
notesService.getText().then(function(notes) {
console.log(notes);
});
$http.get
returns a Promise.
Since the promise callback in then
is async, you need to handle the promise in the controller. To do this, first return the promise in the factory:
return $http.get('www/getNotes.php') <-- return added at the beginning of this line
.then(function (payload){
return payload.data;
});
And then, handle the promise in the controller:
$scope.updateFunc = function () {
notesService.getText().then(function(notes) {
console.log(notes);
});;
};
the problem is that you are not returning anything on the getText function. If you return the promise from the $http.get the you should implement any of the promise methods to fulfill your model variable notes
diary.factory('notesService', function ($http) {
return {
getText: function () {
return $http.get('www/getNotes.php')
.then(
function (payload){
return payload.data;
});
}
}
});
本文标签: javascriptFunction Returning Undefined AngularJSStack Overflow
版权声明:本文标题:javascript - Function Returning Undefined AngularJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744762676a2623846.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论