以下是問題 - 當可用數字爲0時,可以爲用戶分配有限數量的許可證,但不能再指定其他按鈕,並且其他按鈕將被禁用。許可證可以刪除並重新分配。如何在ngRepeat中保留AngularJS組件的總數
用戶列表位於ngRepeat循環中,分配/刪除許可證功能位於組件中。當我點擊分配/移除按鈕時,它會自行更新和總計,但其他組件中的按鈕不會更新,直到下一次單擊。
這裏是我到目前爲止的全部代碼:http://plnkr.co/edit/T4soR8qpSAzY0cANknsE?p=preview
的HTML:
<body ng-controller="RootController as root">
<pre>qty: {{ root.qtyAvailable }}/{{ root.qtyMax }}</pre>
<div ng-repeat="user in root.users | orderBy: 'firstname' ">
{{ user.firstname }}
<assign
has-licence="user.hasLicence"
reassignable="user.reassignable"
qty="root.qtyAvailable"
qty-max="root.qtyMax"
></assign>
</div>
</body>
控制器和組件:
.controller('RootController', function() {
this.qtyMax = 2;
this.qtyAvailable = 1;
this.users = [
{firstname: 'john', hasLicence: false, reassignable: true},
{firstname: 'jane', hasLicence: false, reassignable: true},
{firstname: 'joey', hasLicence: false, reassignable: true},
{firstname: 'bob', hasLicence: true, reassignable: true},
];
})
.component('assign', {
template: `<button ng-click="$ctrl.click($ctrl.hasLicence)">{{ $ctrl.text }}</button>`,
controller: function() {
this.text = '';
// set the button text
this.buttonText = function() {
if(this.hasLicence) {
this.text = 'remove';
}
else if(!this.hasLicence && this.reassignable && this.qty>0) {
this.text = 'assign';
}
else {
this.text = '-'; // eg button disabled
}
}
this.buttonText();
// click function
this.click = function(licence) {
if(licence === true) {
this.hasLicence = false;
this.qty++
}
else if(this.qty>0) {
this.hasLicence = true;
this.qty--
}
this.buttonText(this.hasLicence);
console.log(this.qty)
}
},
bindings: {
hasLicence: '<',
reassignable: '<', // not relevant for this demo
qty: '=',
qtyMax: '<'
}
});
真棒,謝謝。這正是我試圖實現的目標 – EdwardJPayton