【发布时间】:2017-01-07 01:02:35
【问题描述】:
为了创建/编辑网站内容,我有一个带有一些选项卡的表单(按钮使选项卡系统的想法,因为用户可能想要添加另一个“选项卡”等),我使用指令来显示选定的标签:
<form name="form" novalidate>
<div class="tabsContainer">
<!--some button making the idea of tabs system-->
</div>
<div class="contentDataContainer">
<content-form ng-model="VM.CurrentContent" page-features="VM.PageFeatures"></content-form>
</div>
</form>
当用户想要编辑内容时,我向网络服务请求内容数据:
loadData = function () {
var deferred = $q.defer();
ContentsService.GetContentToCreateEdit({ id: vm.IdContent })
.then(function(response)
{
try {
if (vm.IdContent <= 0 || (response.ResultOperation === ResultEnum.Success && exists(response.ResultObject.Contents)))
{
vm.Model = ContentVM.GetModelToShow(response.ResultObject.Contents);
vm.CurrentContent = vm.Model[0];
}
else
{
//some like with error messages
}
}
catch (err) { }
deferred.resolve();
}, function () {
deferred.resolve();
});
LoadingService.Load(deferred.promise, 'first');
};
并且指令的模型必须更新。
指令控制器:
vm.Model = { Id: 0 };
function init()
{
vm.Model = exists($scope.model) ? $scope.model : getDefaultContentInfo();
if(isStringEmpty(vm.Model.Price)) vm.Model.Price = "";
vm.Model.PriceText = vm.Model.Price;
vm.Model.IsIntervalDate = !isStringEmpty(vm.Model.EndDate) && vm.Model.EndDate != vm.Model.StartDate;
};
init();
$scope.$watch($scope.model, function () {
init();
}, true);
如果我在init() 函数中添加debugger,则指令会完美地更新为$scope.model,但如果我没有,它不会...
有人可以解释这里发生了什么,我该如何解决?
我已经尝试使用$scope.$apply(),但出现摘要错误,我已经尝试使用$timeout(),但指令的模型没有更新。
$timeout(function () {
init();
$scope.$watch($scope.model, function () {
init();
}, true);
},500);
【问题讨论】:
标签: angularjs angularjs-directive angularjs-scope