0

How to apply class based on condition isErrored to be true or false.

The below code is not working:

$scope.addWork = function() {
    var isErrored = false;
    $rootScope.$on('isErrored', function(event, data) { 
        alert(data);
        if(data == true)
            isErrored = true;
    });
    ngDialog.open({
        scope: $scope,
        template: 'addWorkingDialog',
        controller: 'addWorkingController',
        className: isErrored ? 'ngdialog-theme-default 
         alertmsgDialog' : 'ngdialog-theme-default workingDialog'
    });
};
sjain
  • 23,126
  • 28
  • 107
  • 185

1 Answers1

0

You should move $rootScope.$on('isErrored', function(event, data) { ... }); outside $scope.addWork function because it is an event listener, then use isErrored as a $scope property.

$scope.isErrored = false;
$rootScope.$on('isErrored', function(event, data) { 
    if(data == true)
        $scope.isErrored = true;
    else
        $scope.isErrored = false;
});

$scope.addWork = function() {
    ngDialog.open({
        scope: $scope,
        template: 'addWorkingDialog',
        controller: 'addWorkingController',
        className: $scope.isErrored ? 'CLASS_1' : 'CLASS_2'
    });
};
Muhammad Reda
  • 26,379
  • 14
  • 93
  • 105