Вот проблема: ограниченное количество лицензий может быть назначено пользователям, когда доступный номер 0 не может быть назначен, а другие кнопки будут отключены. Лицензии можно удалить и переназначить.Как сохранить общую сумму между компонентами AngularJS в ngRepeat
Список пользователей находится в цикле ngRepeat, а функция assign/remove license находится в компоненте. Когда я нажимаю кнопку назначения/удаления, он обновляет себя и общее количество, но кнопка в других компонентах не обновляется до следующего щелчка.
Вот полный код, что я до сих пор: 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