单击按钮时,ng-template不会刷新

时间:2017-08-17 07:02:27

标签: javascript angularjs

我有以下控制器:

function _deleteServiceInstance(serviceInstanceId) {
    self.hasServerErrors = false;
    self.serverErrors = [];
    var flag = 1;
    if (confirm("Are you sure you want to delete the service instance in this environment region?")) {
        for (var i = 0; i < self.serviceInstanceDeployments.length; i++) {
            var obj = self.serviceInstanceDeployments[i];
            if (obj.state === "ACTIVE")
                flag = 0;
        }
        if (flag === 0)
        {
            self.hasServerErrors = true;
            self.serverErrors = ["Please delete the active deployments before deleting the service instance"];
            // $window.location.reload();
            $scope.$apply();
            return self.serverErrors;
        };
    }
}

以下是HTML:

   <a uib-popover-template="ctrl.deleteServiceInstanceTemplate" popover-title="Delete service instance"
           popover-placement="auto bottom" popover-trigger="outsideClick">
            <button type="button" class="btn btn-danger">
                Delete
            </button>
   </a>

<script type="text/ng-template" id="deleteServiceInstance.html">
    <div class="container-fluid">
        <div class="row">
            <form name="ctrl.delete.form" class="form-horizontal" novalidate>
                <div class="form-group" ng-class="{'has-error' : ctrl.hasServerErrors}">
                    <label class="col-sm-12 control-label">Are you sure?
                        <button type="submit" class="btn btn-primary" ng-click="ctrl.deleteServiceInstance(ctrl.serviceInstance.id)"
                                ng-disabled="ctrl.purge.form.$invalid">Yes</button>
                        <span ng-show="ctrl.hasServerErrors" class="help-block" ng-repeat="serverError in ctrl.serverErrors">{{serverError}}</span>
                    </label>
                </div>
            </form>
        </div>
    </div>
</script>

基本上,有一个删除按钮作为弹出窗口打开。如果我确定要删除某些内容,它会要求确认。如果我按是,它会检查是否有任何处于活动状态的部署。如果有,则显示错误。现在,问题是,当我第二次点击删除按钮时,即使我没有按下是,也会显示相同的错误。在刷新页面之前,它会一直显示相同的错误。

编辑:我在我的控制器中声明了_deleteServiceInstance:

self.deleteServiceInstance = _deleteServiceInstance

1 个答案:

答案 0 :(得分:0)

  

您应该在点击delete按钮事件时清除消息,而不是yes按钮点击事件

<button type="button" ng-click="clearMessages" class="btn btn-danger">
                Delete
            </button>

控制器

function _clearMessages() {
    self.hasServerErrors = false;
    self.serverErrors = [];
}


self.clearMessages= _clearMessages