gpt4 book ai didi

javascript - 无法再次获得 Popup 模式 - AngularJS

转载 作者:行者123 更新时间:2023-11-30 12:03:00 25 4
gpt4 key购买 nike

我目前正在向自己介绍 AngularJS,我正在为一个按钮编写代码,单击该按钮会弹出一个模式。模式很好地弹出。当我点击模式上的“X”时,它也会消失。当设置为 false 以使模态弹出时的 bool 值再次变为 true,一切顺利,但模态不会第二次弹出。为什么会这样?

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">&times;</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;
});
});
}
};
});

我坚持了几个小时!有什么我想念的吗?任何见解都会有所帮助。谢谢

最佳答案

我认为您应该为 $scope.showModal 使用对象而不是简单值.示例:

Controller :

$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">&times;</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;
});
});
}
};
});

当您将对象绑定(bind)到指令时,您可以从指令中更改其属性,这些更改也会反射(reflect)到 Controller 中。

您的代码的其他问题是它的指令有 replace: true - 因此,当您在 View 中声明时 <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
},

关于javascript - 无法再次获得 Popup 模式 - AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36160418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com