1
在我的html頁面正確開啓和關閉引導模式,我只是一個簡單的引導模式和按鈕來調用它:如何從AngularJS
<button class="btn btn-primary" data-toggle="modal" data-target="#addUpdateGroup">Add group</button>
<div class="modal fade" id="addUpdateGroup" tabindex="-1" role="dialog" aria-labelledby="addUpdateGrouplLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
</div>
<form name="addUpdateGroupForm" ng-submit="addUpdateGroupForm.$valid && groupCtrl.addUpdateGroup()" novalidate>
<div class="modal-body">
<input type="hidden" ng-model="groupCtrl.group.id" name="groupid" />
<label for="groupname">Group name:</label>
<br>
<input ng-model="groupCtrl.group.name" type="text" name="groupname" id="groupname" required />
<br><br>
<label for="groupcolor">Boja grupe:</label>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Add</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</form>
</div>
</div>
</div>
這是一個只是簡單的形式。驗證由AngularJS完成。
現在......當用戶提交表單時,我想讓模式關閉,但是隻有當它是有效的輸入時。
所以我把代碼用於關閉模態到控制器:
this.addUpdateGroup = function() {
// Adding a new group
if (typeof this.group.id === 'undefined') {
this.id += 1;
this.group.id = this.id;
this.groups.push(this.group);
// Updating an existing group
} else {
this.groups[this.group.id].name = this.group.name;
this.groups[this.group.id].color = this.group.color;
}
// Clean the form and remove the modal
this.group = {};
$scope.addUpdateGroupForm.$setPristine(true);
$('.modal').modal('hide');
};
但這並不遵循沒有操縱從控制器的DOM的最佳實踐。
問題:有沒有更好的方法?當控制器中的某個函數被調用時,如何實現需要顯示或隱藏的模式?
你不使用angular-ui bootstrap模態嗎? – PSL 2014-09-28 21:26:34
不是你的意思嗎? http://angular-ui.github.io/bootstrap/它看起來像它仍在操縱控制器中的DOM,對吧? – CodeVirtuoso 2014-09-28 21:40:55
NO。如果您使用angulal ui bootstrap,則不需要自己訪問DOM或任何東西。你只是處理模態對象和承諾。 – PSL 2014-09-28 21:43:10