我目前正在向AngularJS介绍自己,我正在编写一个按钮的代码,点击该按钮会弹出一个模态。模态弹出很好。当我点击' X'在模态上,它也消失了。设置为false以使模式弹出的布尔值再次变为真,一切顺利但模态不会第二次弹出。为什么会这样?
HTML:
<input type="button" ng-click="toggleModal()" class="btn btn-default" value="Path">{{showModal}}
<modal title="Select a Testcase" visible="showModal">
<form role="form" >
<div class="form-group" style="padding: 5%">
<div id="tree_div"></div>
</div>
<input type="button" value="Submit" class="btn btn-default">
</form>
</modal>
JAVASCRIPT:
$scope.showModal = false;
$scope.toggleModal = function(){
$scope.showModal = !$scope.showModal;
};
指令:
'use strict';
angular.module('chariot').directive('modal', function () {
return {
template: '<div class="modal fade">' +
'<div class="modal-dialog">' +
'<div class="modal-content">' +
'<div class="modal-header">' +
'<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>' +
'<h4 class="modal-title">{{ title }}</h4>' +
'</div>' +
'<div class="modal-body" ng-transclude></div>' +
'</div>' +
'</div>' +
'</div>',
restrict: 'E',
transclude: true,
replace:true,
scope:true,
link: function postLink(scope, element, attrs) {
scope.title = attrs.title;
scope.$watch(attrs.visible, function(value){
if(value == true)
$(element).modal('show');
else
$(element).modal('hide');
});
$(element).on('shown.bs.modal', function(){
scope.$apply(function(){
scope.$parent[attrs.visible] = true;
});
});
$(element).on('hidden.bs.modal', function(){
scope.$apply(function(){
scope.$parent[attrs.visible] = false;
});
});
}
};
});
我被困在这几个小时!有什么东西我错过了吗?任何见解都会有所帮助。感谢
答案 0 :(得分:3)
我认为你应该使用对象而不是$scope.showModal
的简单值。例如:
控制器:
$scope.showModal = {visible: false};
$scope.toggleModal = function(){
$scope.showModal.visible = !$scope.showModal.visible;
};
查看:
<input type="button" ng-click="toggleModal()" class="btn btn-default" value="Path">{{showModal}}
<modal title="Select a Testcase" show-modal="showModal">
<form role="form" >
<div class="form-group" style="padding: 5%">
<div id="tree_div"></div>
</div>
<input type="button" value="Submit" class="btn btn-default">
</form>
</modal>
指令:
angular.module('chariot').directive('modal', function () {
return {
template: '<div class="modal fade">' +
'<div class="modal-dialog">' +
'<div class="modal-content">' +
'<div class="modal-header">' +
'<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>' +
'<h4 class="modal-title">{{ title }}</h4>' +
'</div>' +
'<div class="modal-body" ng-transclude></div>' +
'</div>' +
'</div>' +
'</div>',
restrict: 'E',
transclude: true,
replace:true,
scope:{
title: '@',
showModal: '=',
},
link: function postLink(scope, element, attrs) {
if( typeof( scope.showModal.visible ) === "undefined" ) {
scope.showModal.visible = false;
}
scope.$watch('showModal.visible', function(value){
if(value == true)
$(element).modal('show');
else
$(element).modal('hide');
});
$(element).on('shown.bs.modal', function(){
scope.$apply(function(){
scope.showModal.visible = true;
});
});
$(element).on('hidden.bs.modal', function(){
scope.$apply(function(){
scope.showModal.visible = false;
});
});
}
};
});
将object绑定到指令时,可以从指令更改其属性,这些更改也将反映到控制器。
您的代码的其他问题是它的指令有replace: true
- 所以当您在视图中声明<modal title="Select a Testcase" show-modal="showModal">
时 - 此数据将被指令html替换,并使该属性不再可用。
为了解决这个问题,我制定了指令,通过更改映射来获取范围中的值,而不是属性:
scope:{
title: '@', // Use the title attribute value as a string
showModal: '=', // use the "show-modal" attribute as an object
},