在这种AngularJS控制器预填充形式,获取数据是通过使用$广播和$上的事件触发。但是,当数据更新时,只有一些相应的字段在视图中得到更新。
$scope.currentHouse = {};
$scope.$on('houseInfoLoad', function(event, data) {
$scope.currentHouse = data.houseDetail; //does not refresh the view
console.log($scope.currentHouse); //output is correct
//the next three calls refresh the corresponding fields in the view
$scope.changeGarageRadioValue($scope.currentHouse.hasGarage);
utils.setSelection($scope.houseKeywords, $scope.currentHouse.keyword.id);
utils.setSelection($scope.houseTypes, $scope.currentHouse.type.id);
});
changeGarageRadioValue()
基本上做什么它说,和utils.setSelection(list, id)
增加了selected = true
属性具有ID列表的元素。这具有设置选择字段的值的效果(使用isteven的multi-select plugin)。
视图具有结合到的$scope.currentHouse
属性,以及这些单选按钮和选择字段几个文本字段。
结果是文本字段有时不得到更新。但是,选择和单选按钮做。
这是我曾尝试:
- 在
$timeout()
结束语一切; - 设置
$scope.currentHouse
后调用$scope.$apply()
(抛出一个错误,说我们已经内部的$apply()
) - 从
{}
更改$ scope.currentHouse的初始定义与每个设置为空字段的对象。
任何人都可以看到我缺少什么,或者如何强制触发刷新?
EDIT:从视图中提取:
文本字段:
<label>ADDITIONAL DESCRIPTION</label>
<div>
<input type="text" ng-model="currentHouse.additionalDescription" class="input-mandatory" value=""/>
</div>
多选:
<label>TYPE</label>
<div>
<div directive-id="houseType" multi-select input-model="houseTypes" output-model="currentHouse.type" button-label="text" item-label="text" selection-mode="single" default-label="Select" tick-property="selected" ></div>
</div>
单选按钮:
<div><label>HAS GARAGE</label></div>
<div>
<div id="radiobuttonNo" ng-click="changeGarageRadioValue(false);">
NO
</div>
<div id="radiobuttonYes" ng-click="changeGarageRadioValue(true);" class="active">
YES
</div>
</div>
编辑#2按阿洛克·辛格的建议:
我试图把data.houseDetail
为$scope.currentHouse.house
,而不是直接$scope.currentHouse
,并相应地改变了看法。仍然没有结果。
你能告诉我们您的视图代码? – Guinn
我已经添加了摘录。实际的视图有点复杂,它是一个模态窗口,所以控制器和视图是从父视图调用的。 – foucdeg