0

I will use the Data from GET Request in Angular.js, like below.

    var app = angular.module('Saidas',[]);
    app.controller('Status', function($scope, $http, $interval) {
        $interval(function(){
            var request = $http.get('/SaidasSite');    
            request.success(function(data) {
                if (data='QTD1LEDON') {
                    $('#QTD1LAMP').bootstrapToggle('on');
                } else (data='QTDLEDOFF'){
                    $('#QTD1LAMP').bootstrapToggle('off');
                } else (data='QTD2LEDON'){
                    $('#QTD2LAMP').bootstrapToggle('on')
                } else { $('#QTD1LAMP').bootstrapToggle('on'); }
            })
            .error(function(data){
                console.log('Error: ' + data);
            });
        }, 5000); 

When i receive "QTD1LEDON", i will toogle a Button in my Code to "ON", when i receive QT1LEDOFF, i will toogle to "OFF". But when i do this code, the comparison always a "QTD1LEDON". What am I doing wrong?

Sorry for my bad english

Luiz Henrique
  • 103
  • 1
  • 11

4 Answers4

1

You are making an assigment instead a comparison. So, in the first if you are giving to data the value 'QTD1LEDON', and always is true.

You need to fix:

if (data='QTD1LEDON') {

with:

if (data==='QTD1LEDON') {
Ramon-san
  • 1,007
  • 13
  • 25
0

You should use === for comparison, not = which is for assignment.

NicolasMoise
  • 7,261
  • 10
  • 44
  • 65
0

In javascript you make a comparison using === or ==.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators

In your code you use =, which simply assigns a value to your data variable, meaning the condition always evaluates as truthy: if (data='QTD1LEDON') {

You need to use == or === and it will behave as you are expecting:

if (data === 'QTD1LEDON') {

iamalismith
  • 1,531
  • 9
  • 22
0

You are making assignments instead of comparisons and you also have 3 else statements where you should only have 1, so you should have something like

if (data === 'QTD1LEDON') {
     ...
} else if (data === 'QTDLEDOFF') {
     ...
} else if (data === 'QTD2LEDON') {
     ...
} else { ... }
taguenizy
  • 2,140
  • 1
  • 8
  • 26