编辑:
Plunker正在工作,实际代码不起作用:
http://plnkr.co/edit/5oVWGCVeuTwTARhZDVMl?p=preview
该服务包含典型的getter \ setter内容,此外,它的功能还不错,所以我没有发布它的代码来避免TLDR。
TLDR版本:尝试将用AJAX提取的值ng-init初始化为文本区域的ngModel,请求将解析为正确的值,但文本区域仍为空。
父控制器功能(与服务对话):
$scope.model.getRandomStatus = function(){
var deffered = $q.defer();
var cid = authService.getCompanyId();
var suggestions = companyService.getStatusSuggestions(cid);
if(suggestions && suggestions.length > 0){
deffered.resolve(suggestions[Math.floor(Math.random(suggestions.length) + 1)].message);
return deffered.promise;//we already have a status text, great!
}
//no status, we'll have to load the status choices from the API
companyService.loadStatusSuggestions(cid).then(function(data){
companyService.setStatusSuggestions(cid, data.data);
var result = data.data[Math.floor(Math.random(data.data.length) + 1)];
deffered.resolve(result.message);
},
function(data){
_root.inProgress = false;
deffered.resolve('');
//failed to fetch suggestions, will try again the next time the compnay data is reuqired
});
return deffered.promise;
}
子控制器:
.controller('shareCtrl', function($scope){
$scope.layout.toggleStatusSuggestion = function(){
$scope.model.getRandomStatus().then(function(data){
console.log(data);//logs out the correct text
//$scope.model.formData.shareStatus = data;//also tried this, no luck
return data.message;
});
$scope.model.formData.shareStatus = $scope.layout.toggleStatusSuggestion();//Newly edited
}
});
HTML:
<div class="shareContainer" data-ng-controller="shareCtrl">
<textarea class="textAreaExtend" name="shareStatus" data-ng-model="model.formData.shareStatus" data-ng-init="model.formData.shareStatus = layout.toggleStatusSuggestion()" cols="4"></textarea>
</div>
最佳答案
我相信您想要的是:
$scope.model.getRandomStatus().then(function(data){
$scope.model.formData.shareStatus = data.message;
});
从
then
内部返回内容不会从包装它的函数中返回任何内容,因此不执行任何操作