var DialogController = function ($scope, configFac, $q) {
var placeholders = [];
var varInit = function () {
$q.all([configFac]).then(function (response) {
$scope.configResources = response[0];
placeholders[0] = response[0].one;
placeholders[1] = response[0].two;
placeholders[2] = response[0].three;
});
};
var states = {
'initial': {
'key': 'initial',
'inputPlaceholder': placeholders[0],
'avatar': {
'state': 'default'
}
},
'editing': {
'key': 'editing',
'inputPlaceholder': placeholders[1],
'avatar': {
'state': 'listening'
}
},
'answered': {
'key': 'answered',
'inputPlaceholder': placeholders[2],
'avatar': {
'state': 'thinking-complete'
}
}
};
)};
因此,当我使用我的项目运行此代码时,inputplaceholders被设置为空,因为promise没有足够快地返回。有没有办法让它等待?另外,解决这个问题的另一种方法是在项目开始时运行介绍代码,那么是否有一种方法可以从另一个模块进行调用,以便在代码运行时运行?
答案 0 :(得分:0)
在promise then
中为状态对象设置新值:
var DialogController = function ($scope, configFac, $q) {
var placeholders = [];
var varInit = function () {
$q.all([configFac]).then(function (response) {
$scope.configResources = response[0];
placeholders[0] = response[0].one;
placeholders[1] = response[0].two;
placeholders[2] = response[0].three;
states.initial.inputPlaceholder = placeholders[0];
states.editing.inputPlaceholder = placeholders[1];
states.answered.inputPlaceholder = placeholders[2];
});
};
var states = {
'initial': {
'key': 'initial',
'avatar': {
'state': 'default'
}
},
'editing': {
'key': 'editing',
'avatar': {
'state': 'listening'
}
},
'answered': {
'key': 'answered',
'avatar': {
'state': 'thinking-complete'
}
}
};
)};