0

I have a table that contains a button that activates/deactivates users. When I click that button it makes a API call to change the status of the user. What I am having trouble with is performing a live update with the view after the API call. Currently, the only way I see the text switch is when I refresh the page.

Here is my Admin.html where the text on the button should switch between 'Active' and 'Inactive' when the button is clicked.

<tr ng-repeat="account in $ctrl.account">
  <td>{{account.FirstName}}</td>
  <td>{{account.LastName}}</td>
  <td class="text-center">
    <button class="btn btn-sm btn-active-status" ng-class="account.Active === true ? 'btn-info' : 'btn-danger'" ng-bind="account.Active === true ? 'Active' : 'Inactive'" ng-click="$ctrl.UpdateActiveStatus(account.Id, account.Active)"></button>
  </td>
</tr>

Here is my AdminController.js

app.component('admin', {
    templateUrl: 'Content/app/components/admin/Admin.html',
    controller: AdminController,
    bindings: {
        accounts: '='
    }
})

function AdminController(AccountService) {
    this.$onInit = function () {
        this.account = this.accounts;
    }

    this.UpdateActiveStatus = function (id, status) {
        var data = {
            Id: id,
            Active: !status
        };

        AccountService.UpdateActiveStatus(data).then(function (response) {
            AccountService.GetAccounts().then(function (data) {
                this.account = data;
            });
        });
    };
}
Maria Ines Parnisari
  • 16,584
  • 9
  • 85
  • 130
Ron T
  • 397
  • 1
  • 4
  • 22
  • After the successful api call, why are you refreshing the entire table? You should update just the object of the account with the given id – Maria Ines Parnisari Apr 07 '17 at 02:22
  • don't use `this`, as it's context changes depending on which function called it. You should instead create an alias to `this` (i.e. `var controller = this;` then in the callback `controller.account = data`), or use ES6 Arrow functions, or `.bind()` – Claies Apr 07 '17 at 02:52
  • 1
    Possible duplicate of [How to access the correct \`this\` inside a callback?](http://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback) – Claies Apr 07 '17 at 02:55
  • Good idea, I will do that @MariaInesParnisari – Ron T Apr 07 '17 at 05:50
  • thank you for the tip, that made it work! @Claies – Ron T Apr 07 '17 at 05:50

1 Answers1

0

Here is the fix to my problem. Please let me know if there is a better way than this.

function AdminController(AccountService) {
    var controller = this;
    this.$onInit = function () {
        controller.account = this.accounts;
    }

    this.UpdateActiveStatus = function (id, status) {
        var data = {
            Id: id,
            Active: !status
        };

        AccountService.UpdateActiveStatus(data).then(function (data) {
            for (var i = 0; i < controller.account.length; i++) {
                if (controller.account[i].Id === data.Id) {
                    controller.account[i].Active = data.Active;
                }
            }
        });
    };
}
Ron T
  • 397
  • 1
  • 4
  • 22