刷新Angular-ui bootstrap $ scope

时间:2015-06-03 21:40:45

标签: javascript angularjs angular-ui-bootstrap

所以我让他们说:

$scope.errorMessage = "error1";
$scope.addCardModal = $modal.open({ 
    animation: true, 
    templateUrl: 'pages/templates/modals/modal.html',
    size: "lg",
    scope: $scope
});

$scope.checkError = function() {
    $scope.errorMessage = "another error";
} 

在我的modal.html模板中,我有:

<div ng-click="checkError()">Check Error</div>
<div ng-show="errorMessage">{{ errorMessage }}</div>
<div ng-click="errorMessage = false">Close</div>
  • 当我加载我的模态时,错误消息显示它应该是。
  • 当我点击关闭时,错误消息会隐藏,就像它应该的那样。
  • 但是,当我点击检查错误时,$scope会发生变化,但不会转换回模态。

有谁知道为什么会发生这种情况以及如何让$scope回到模态中?因为如果我关闭模态并将其重新打开,则会再次显示正确的错误消息。

2 个答案:

答案 0 :(得分:2)

试试这个:

$scope.errorMessage = "error1";
$scope.addCardModal = $modal.open({ 
    animation: true, 
    templateUrl: 'pages/templates/modals/modal.html',
    size: "lg",
    scope: $scope
});

$scope.checkError = function() {
    $scope.errorMessage = "another error";
}

$scope.toggleError = function(){
    $scope.errorMessage = "";
}

HTML:

<div ng-click="checkError()">Check Error</div>
<div ng-show="errorMessage">{{ errorMessage }}</div>
<div ng-click="toggleError()">Close</div>

我添加了一个toggleError()函数,以便清空errorMessage字符串。

答案 1 :(得分:1)

您是否尝试过以下操作:

$scope.checkError = function() {
  $scope.errorMessage = "another error";
  $scope.$apply();
}

也许没有触发$ digest。

编辑: 问题不在于$ digest,而是在于修改不同的范围。 创建模态时,Angular会为其分配子$ scope,因此单击此按钮时:

<div ng-click="errorMessage = false">Close</div>

一个新的&#39; errorMessage&#39;变量是在模态范围内创建的。因此,即使父作用域中的errorMessage变量有更新,模式也会在其作用域中优先使用errorMessage。