如何使用ng-show显示元素,我可以使用布尔值控制我的控制器? (控制器中的所有内容都可正常工作,除了此按钮)
这是我的模态html。
<div class="modal-box modal-confirm-pickup-box" ng-show="showPickupModal">
<div class="modal-container modal-confirm-pickup-container">
<div class="modal-container-content">
<div class="modal-container-content-header">
<strong>
Login
</strong>
</div>
<div class="modal-container-content-body">
aaaaaaaeoa
</div>
</div>
</div>
</div>
这是我在控制器中的代码
.controller('venderConsole', ['$scope','$http', function ($scope,$http) {
$scope.showPickupModal = false;
$('.close-modal-box-bt').click(function(event) {
/* Act on the event */
$('.modal-box-login').css('display','none');
$.ajax({
url: 'json/multipleOrder.json', //Server script to process data
type: 'get',
beforeSend: function(xhr){
},
success: function(data){
$scope.orders = data.data
// $scope.ordersList = $scope.orders[0].products;
// console.log(orderSize);
$scope.getTotal = function(order){
var total = 0;
for(var i = 0; i < order.products.length; i++){
var subtotal = order.products[i].subtotal;
total += subtotal;
}
return total;
}
$scope.$apply();
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest);
alert(textStatus);
}
});
});
}]);
这是我的按钮html。
<table id="front-table" class="table table-bordered" ng-show="!vendorPageToggle">
<thead>
<tr class="table-head-text-center">
<th></th>
<th>Order</th>
<th>฿ Total</th>
<th>Address</th>
<th>Comment</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="order in orders">
<th class="text-center">
<span class="table-order-number">
<strong>{{ order.id }}</strong>
</span>
<br>
<span>{{ order.customer.name }}</span>
<br>
<br>
<span>{{ order.estimated_pickup }}</span>
</th>
<td>
<span ng-repeat="product in order.products">
1 X {{ product.name }} <br>
<span ng-repeat="extra in product.extras">
<span ng-repeat="item in extra.items">
-- ext {{ item }}
<br>
</span>
</span>
<br>
</span>
</td>
<td>{{ getTotal(order) }}</td>
<td>
{{ order.customer.address.address1 }}
<br>
{{ order.customer.address.address2 }}
<br>
{{ order.customer.address.suburb }}
<br>
{{ order.customer.address.city }}
<br>
<br>
<strong>
<i class="icon ion-iphone" style="font-size: 24px;"></i> {{ order.customer.phone }}
</strong>
</td>
<td>aaaa</td>
<td class="text-center">
<button class="button button-balanced order-bt" ng-click="showPickupModal = !showPickupModal">
button-balanced
</button>
</td>
</tr>
</tbody>
</table>
我的Css
.modal-box {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 1050;
/*display: none;*/
overflow: hidden;
-webkit-overflow-scrolling: touch;
outline: 0;
background-color: rgba(0, 0, 0, 0.5);
}
.modal-container {
position: relative;
width: auto;
margin: 10px;
}
.modal-container-content {
position: relative;
background-color: #fff;
-webkit-background-clip: padding-box;
background-clip: padding-box;
border: 1px solid #999;
border: 1px solid rgba(0,0,0,.2);
border-radius: 20px;
outline: 0;
overflow: hidden;
}
.modal-container-content-header {
background-color: #FF0000;
color: #fff;
text-align: center;
font-size: 32px;
height: 80px;
padding-top: 28px;
}
.modal-container-content-body {
padding: 15px;
background-color: #EEEEEE;
}
但是当我点击我的按钮时没有发生任何事情,我的模态仍然不像开头一样。
我该如何解决这个问题。 谢谢!
答案 0 :(得分:2)
以下是您的代码按预期工作:JS Fiddle
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
<script>
var app = angular.module('app', []);
app.controller('ctrl', function ($scope) {
$scope.hello = 'Ohh, hello there!';
$scope.showPickupModal = false;
});
</script>
<div ng-app='app' ng-controller='ctrl'>
<button class="button button-balanced order-bt" ng-click="showPickupModal = !showPickupModal">
button-balanced
</button>
<div class="modal-box modal-confirm-pickup-box" ng-show="showPickupModal">
<div class="modal-container modal-confirm-pickup-container">
<div class="modal-container-content">
<div class="modal-container-content-header"> <strong>
Login
</strong>
</div>
<div class="modal-container-content-body">{{hello}}</div>
</div>
</div>
</div>
</div>
如果这不能解决您的问题,请发布更多代码,以便我们可以看到您的控制器无法正常工作的错误。
使用新的完整代码进行编辑
好的,我不知道为什么你的代码不起作用。但是,只是为了测试,我喜欢不对视图做任何事情。所以,我已经删除了html上的代码并通过函数进行了交换。像这样:
ng-click='showPickupModal = !showPickupModal'
这被删除了:
ng-click='togglePickupModal()'
并且,在控制器上,我添加了这个函数,并且在任何console.log测试之前,它只是工作。
$scope.togglePickupModal = function () {
$scope.showPickupModal = !$scope.showPickupModal;
};
你没有显示'.close-modal-box-bt'的代码,所以我也使用了切换功能来关闭模态。问题解决了。