Luiz Henrique Luiz Henrique - 10 months ago 47
Javascript Question

How to equals correctly a Variable with String in Angular.js

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) {
var request = $http.get('/SaidasSite');
request.success(function(data) {
if (data='QTD1LEDON') {
} else (data='QTDLEDOFF'){
} else (data='QTD2LEDON'){
} else { $('#QTD1LAMP').bootstrapToggle('on'); }
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

Answer Source

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') {


if (data==='QTD1LEDON') {