Reputation: 4517
View:
<html ng-app="myApp">
....
<body>
<div ng-controller="myCtrl">
<p>Current user {{loggedOnUser}}</p>
<div ng-if="user.name == {{loggedOnUser}}" ng-repeat="user in users">
<p>Should display</p>
</div>
</div>
</body>
</html>
Controller:
angular.module("myApp", [])
.controller("myCtrl", function($scope){
$scope.loggedOnUser = "xxx"; // for testing purpose
$scope.users = [
{
name : "xxx",
age: "25"
},
{
name : "yyy",
age: "26"
}
];
});
How to use ng-if with angularJS expression or is there any other way to achieve this?? I want to show that div if my loggedOnUser is equal to user.name
Thanks in advance
Upvotes: 2
Views: 15114
Reputation: 4655
It should be
ng-if="user.name == loggedOnUser"
instead of
ng-if="user.name == {{loggedOnUser}}"
You can user filters
if you want to. But I suggest you to not trust on filters in your case. Please see below snippet, it will work in your above scenario.
<div ng-repeat="user in users | filter:name:loggedOnUser">
Upvotes: 8
Reputation: 1925
The condition for ng-if is an angular expression itself.
<ANY
ng-if="expression">
...
</ANY>
therefore something like:
ng-if="someVal"
will match for $scope.someVal and not against the string "someVal"
so in your case it should be:
ng-if="user.name == loggedOnUser"
Upvotes: 3