nolags nolags - 1 year ago 80
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:


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

But this does not work. Can anyone help?

Answer Source

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'}];


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 ( != 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.