-1

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?

nolags
  • 633
  • 1
  • 11
  • 30

1 Answers1

1

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255