nolags nolags - 25 days ago 12
AngularJS Question

show different uib-alerts by using a if function ANGULARJS

How can I show different alerts by using a if function which gets its variable from nodejs backend?
In my html stands:

<div uib-alert ng-repeat="alert in alerts" type="alert.type" close="closeAlert($index)">{{alert.msg}}</div>


in my AngularJS file:

$http.get('/test').then(function(result){

this.alerts = function(){
if(result.test != null){
[ { type: 'danger', msg: 'Test is false' }];
}else{
[ { type: 'success', msg: 'Test is right' }];
}
};
}


But this does not work. Can anyone help?

Answer

Your code is not valid JavaScript. It would be obvious if you looked for errors in the browser console.

Here's valid code:

var test = false;

if (!test) {
    this.alerts = [{type: 'danger', msg: 'Test is false'}];
} else {
    this.alerts = [{type: 'success', msg: 'Test is right'}];
}

EDIT:

And again, in your edited code, it's still not valid JavaScript. It should be

var that = this;
$http.get('/test').then(function(response) {
    if (response.data.test != null) {
        that.alerts = [{type: 'danger', msg: 'Test is false'}];
    } else {
        that.alerts = [{type: 'success', msg: 'Test is right'}];
    }
});

You really, really, really need to learn the basic syntax of JavaScript, and to indent your code, before even thinking playing with angular.