Paolo Pedrazzi Paolo Pedrazzi - 1 month ago 8
AngularJS Question

If statement in ionic, angualr js

I have this code in my controller on ionic app

// Shows test score to the user
alertPopup = $ionicPopup.alert({
title: user + message,
template: 'Your score: ' + testScore + '%',
okText: Paid ? 'View test results' : 'Close'
});


i need to add an if where the message change if testScore var is > 50.
But i'm a beginner in ionic or angular js... Can any good guy give me help?

Answer

You can just change your template:

var template = '<div>Your score: ' + testScore +'%</div>';
if (testScore > 50) {
    template += "<div>This only shows if testScore is higher than 50</div>";
}

alertPopup = $ionicPopup.alert({
    title: user + message,
    template: template,
    okText: Paid ? 'View test results' : 'Close'
});

Or, probably even better, handle it in the template in angular's way:

alertPopup = $ionicPopup.alert({
    title: user + message,
    scope: $scope, // the $scope object with the testScore property
    template: '<div>\
                   Your score: {{ testScore }}%>\
               </div>\
               <div ng-if="testScore > 50">\
                   This only shows if testScore is higher than 50\
               </div>',
    okText: Paid ? 'View test results' : 'Close'
});