Luiz Henrique Luiz Henrique - 2 months ago 6
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) {
$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

Answer

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