如果条件满足,我试图隐藏按钮。使用ng-show
这应该很容易,但由于某种原因,我在实现这个方面遇到了困难。
我的代码如下:
HTML
<a href = "example.com" ng-click="Bookings.disableCancelFirstBooking(car)">
<i class="red" ng-show="Bookings.disableCancellationButton">
</i>
</a>
controller.js
this.disableCancellationButton = true;
this.disableCancelFirstBooking = function(carID){
BookingsResource.firstBookingOfSubscription({id: carID}, $.proxy(function () {
if(carID.status === 'waiting'){
this.disableCancellationButton = false;
console.log(this.disableCancellationButton)
console.log('why does this not work?')
}
}));
}
最后是我正在实施的服务
firstBookingOfSubscription: {
method: "GET",
url: 'example/bookings/:id/cars',
isArray: true
}
奇怪的是,我在控制台中看到了'why does this not work?'
消息以及this.disableCancellationButton = false;
为什么图标没有被隐藏的任何想法?
我已阅读了很多SO帖子但找不到相关答案
答案 0 :(得分:3)
回调内部this
改变了范围(ES6 =>
的一个优点是在那里处理了this
。试试这个(没有双关语):
var self = this
this.disableCancellationButton = true;
this.disableCancelFirstBooking = function(carID){
BookingsResource.firstBookingOfSubscription({id: carID}, $.proxy(function () {
if(carID.status === 'waiting'){
self.disableCancellationButton = false;
//^^
console.log(this.disableCancellationButton)
console.log('why does this not work?')
}
}));
}
答案 1 :(得分:1)
为什么不对disableCancellationButton变量使用$ scope。我认为你的变量是更新但不是视图的传输。