athi athi - 5 months ago 33
HTML Question

ng-if condition always fails

I'm using AngularJS

<div ng-repeat="l in kites">
<a ng-click="findit(l.green.num)">color</a>
<span ng-if="c_{{l.green.num}} == 'y'>kites is </span>
</div>


In my controller I have

$scope.cnt=[];
$scope.findit = function(c){
$scope.cnt.push(c);
angular.forEach($scope.cnt, function(value, key){
$scope['c_'+value] = 'y' ;

})
}


My problem is, I'm not getting the span value even the conditions falls true..
means while
l.green.num
is 5 ,
c_5 ='y'
. But it is not accepted in the if condition. Please help me.

Answer

Below is your solution:

var $scope;
var app = angular.module('miniapp', []);

function Ctrl($scope) {
	$scope.kites = [{
  	'green':{
    	'num':1
    }
  }]
  $scope.cnt = [];
  $scope.findit = function(c) {
    $scope.cnt.push(c);
    angular.forEach($scope.cnt, function(value, key) {
      $scope['c_' + value] = 'y';

    })
  }

};
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="miniapp" ng-controller="Ctrl">
  <div ng-repeat="l in kites">
    <a ng-click="findit(l.green.num)">color</a>
    <span ng-show="{{'c_'+l.green.num}} == 'y'">kites is </span>
  </div>
</div>