Search code examples
angularjsangularjs-ng-repeatangular-ng-if

ng-if on ng-repeat value angular js


hello everyone I have a problem I want to change the value of type in ng-repeat

here is the simple example to explain what I want to say

let arr = 
[
  {school_id: 1, name: "jon"},
  {school_id: 2, name: "sam"},
  {school_id: 1, name: "sara"},
  {school_id: 2, name: "youfiy"}
]
<ul>
   <li ng-repeat="arr">arr.school_id</li> <!-- here shuld print school name instide of schoolid -->
</ul>

I need in id 1 change school_id to "primarySchool" and in id 2 change to "hightSchool" and so on


Solution

  • Use ng-switch and add your condition as you need,

    var app = angular.module('test',[]);
    
    app.controller('testCtrl',function($scope){
    
    $scope.arr = 
    [
      {school_id: 1, name: "jon"},
      {school_id: 2, name: "sam"},
      {school_id: 1, name: "sara"},
      {school_id: 2, name: "youfiy"}
    ];
    
    });
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <body ng-app="test" ng-controller="testCtrl">
    <ul>
       <li ng-repeat="schooldObj in arr"> 
         <div ng-switch on="schooldObj.school_id">
          <div ng-switch-when="1">
             PrimarySchool
          </div>
        <div ng-switch-default>
            hightSchool
        </div>
    </div>
     </ul>
    </body>