gpt4 book ai didi

javascript - AngularJS : Factory $http assigning values returning null

转载 作者:行者123 更新时间:2023-12-02 15:55:39 24 4
gpt4 key购买 nike

我试图从 $http.get() 分配一个变量,尽管 conf var 为 null,尽管请求经过并返回 json。

app.factory('Config', ['$http',
function($http) {
return {

conf: null,

init: function () {

if (this.conf === null) {
$http.get('/config')
.success(function (data) {
this.conf = data;
});
}
}
}
}
]);

最佳答案

成功回调函数里面的this是不同的

app.factory('Config', ['$http',
function($http) {
return {

conf: null,

init: function () {
var self = this;
if (this.conf === null) {
$http.get('/config')
.success(function (data) {
self.conf = data;
});
}
}
}
}
]);

关于javascript - AngularJS : Factory $http assigning values returning null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31568653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com