我必须在 Controller 2 addData
API 调用中调用 $route.reload();
,以便我可以在 UI 中获取添加的数据。但随后由于页面刷新,文本“数据添加成功”消失了。
Controller 1:
$rootScope.$on('reloaded', function(event, data) {
$scope.$parent.alerts.length=0;
$scope.$parent.alerts.push({type: 'success',msg: 'Data added successfully'});
});
Controller 2:
$scope.add = function() {
someAPIService.addData(JSON.stringify($scope.rows)).success(function(response) {
ngDialog.close('ngdialog1');
$route.reload();
$rootScope.$emit('reloaded', "true");
});
}
HTML 部分:
<section>
<div ng-controller="controller3">
<alert ng-repeat="alert in alerts" type="{{alert.type}}" close="closeAlert($index)">{{alert.msg}}</alert>
</div>
<div class="row form-inline" ng-controller="controller2 as vm">
<!-- Table data with text fields-->
</div>
<script type="text/ng-template" id="addDataDialog">
<div id="frm" ng-controller="controller1" class="col-xm-6">
<div class="form-group">
<!-- Labels with text fields-->
</div>
<button class="ngdialog-button" ng-click="add()">Save</button>
</div>
</script>
</section>
注意:两个 Controller 位于同一个 JS
文件中,并且它们用于同一个 HTML 文件。
最佳答案
因为您只想加载服务器端的最新记录列表。那么在进行 post 调用后就不需要使用 $route.reload();
了。您只需要进行ajax调用即可获取最新记录列表,这将解决您的问题。制作ajax需要引用$http
$route.reload()
is getting used only when you need to load controller again with specified template in your$routeProvider
when condition
关于javascript - 页面重新加载后显示警报消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30462050/