Struggling to compare two strings in AngularJS, I've come across various examples online. From what I understand, there are different methods like angular.equals(str1, str2), ===, and == if you're certain both are strings...
Despite trying all three approaches, the expected result eludes me. There must be a mistake somewhere in my implementation but pinpointing it is proving to be a challenge.
Upon running the code, the inc1() function gets triggered. The first alert displays "inc1 called". However, the following alert "Inside for loop" only fires once when ideally it should run twice, right?
Moreover, the alert within the if(condition) never seems to execute. Interestingly, removing the 'if' block allows the "Inside for loop" alert to appear twice.
If anyone could shed some light on where I'm going wrong here, I would greatly appreciate it. Despite utilizing angular.equals(), ===, and ==, I seem to encounter the same issue repeatedly.
The HTML and AngularJS snippets are structured as follows:
HTML:
<a class="tab-item" ng-repeat = "x in items" ng-if="name==x.names" ng-click="inc1(name)">
<i class="icon ion-thumbsup"></i>
Like
</a>
AngularJS:
$rootScope.items = [
{ id: 1, names: 'Dolphin', image: 'dolphin.jpg'}, { id: 2, names: 'Donkey', image: 'donkey.jpg'}];
$scope.inc1 = function(name) {
alert("inc1 called");
for(var i=0;i<$rootScope.items.length;i++)
{
alert("Inside for loop");
if (name === $rootScope.items.names[i])
{
alert("If condition satisfied");
}
}
}
//Let's assume name is 'Dolphin'