1

I'm having a weird issue with AngularJS not updating my view when I update the controller vars in a callback:

payment.controller.js

(function () {
  'use strict';
  //TODO: Declare the Cover module
  angular.module('payment', []);

  //TODO: Declare the controller of Cover module
  angular.module('payment').controller('PaymentController', function factory() {
    var _this = this;

    _this.pay = event => {
      event.preventDefault();
      if(_this.Card.Expiration != null && _this.Card.CVC != null && _this.Card.CardNumber != null){
        Stripe.card.createToken({
          number: _this.Card.CardNumber,
          cvc: _this.Card.CVC,
          exp_month: _this.Card.Expiration.split('/')[0],
          exp_year: _this.Card.Expiration.split('/')[1]
        }, myFunc);

        // This doesn't work
        function myFunc(){
          _this.ErrorMessage = 'Error';
        };

        // But this does
        myFunc();
      } else {
        _this.ErrorMessage = 'Check the card data please';
      }
    };
  }
})();

route.js

.when('/payment',{
  templateUrl: 'pay/pay.html',
  controller:'PaymentController',
  controllerAs: 'payment'
})

pay.html (The relevant bit)

<div class="form__errors" ng-show="payment.errorFlag">
  <p>{{payment.ErrorMessage}}</p>
</div>

_this is in scope, and I can see it updating in console. I'm running Angular 1.6.

2
  • How is your view defined? You should try to provide all relevant code. Commented May 9, 2017 at 15:17
  • Sorry, updated. Commented May 9, 2017 at 15:22

1 Answer 1

2

You will need to call $scope.$appy() when the callback has updated your component's model.

Inject $scope into your controller and call $scope.$apply() in myFunc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.