S.Anaconda S.Anaconda - 3 years ago 142
AngularJS Question

angularjs 1.6 two way binding of model doesn't update when changed inside a non angular event

angularjs code



var app = angular.module("testApp", []);
app.controller('captureCtrl', function () {
var vm = this;
vm.obj = {
show: false
};

addressControls.control.listen('populate', function (address, variations) {

vm.line1 = address.Line1;
vm.line2 = address.Line2;
vm.city = address.City;
vm.postcode = address.PostalCode;
vm.obj.show = true;
});

vm.test = function () {
vm.obj.show = true;
}
});


vm.obj.show value in the view doesn't get updated when changed inside the 'populate' event, but it does get updated inside vm.test function. How can I get this to work and why won't binding get updated in the view? I'm using angularjs 1.6.

Answer Source

Try $scope.apply().

$scope.$apply(function () {
      //Your Code here 
    });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download