Skip to content

Instantly share code, notes, and snippets.

@abrarfahad
Last active September 5, 2016 09:51
Show Gist options
  • Save abrarfahad/b348821a70856cfcdd5d51f57b8fcd58 to your computer and use it in GitHub Desktop.
Save abrarfahad/b348821a70856cfcdd5d51f57b8fcd58 to your computer and use it in GitHub Desktop.
set A New Property In A List Of Object. If you wants to view a country name in a city list you can use this gist.
// Code goes here
'use strict';
// Define the `MyApp` module
var app = angular.module('MyApp', []);
app.controller("CityController", function($scope,$filter) {
$scope.Init = function(){
$scope.Countries = [
{"id" : 1, "Name" : "USA" },
{"id" : 2, "Name" : "CANADA" },
];
$scope.Cities= [
{"ID" : 1, "Name" : "MIAMI", "CountryId":1 },
{"ID" : 2, "Name" : "TORONTO", "CountryId":2 },
{"ID" : 3, "Name" : "Ottawa", "CountryId":2 },
{"ID" : 4, "Name" : "Vancouver", "CountryId":2 },
{"ID" : 5, "Name" : "New Yourk", "CountryId":1 },
];
$scope.setListANewProperty( $scope.Cities,$scope.Countries, 'CountryId','id','CountryName','Name');
};
$scope.setListANewProperty = function (listToSetProperty, ListToRead,PropertyNameID,ListToReadID,NewPropertyName,ListToReadPropertyName) {
angular.forEach(listToSetProperty, function(value, key){
if (value[PropertyNameID]!=null || value[PropertyNameID]!=undefined) {
var filter ={};
filter[ListToReadID] = value[PropertyNameID];
var found = $filter('filter')(ListToRead,filter);
if(found.length>0){value[NewPropertyName] = found[0][ListToReadPropertyName];}
}else{
value[NewPropertyName]="--";
}
});
};
});
@abrarfahad
Copy link
Author

abrarfahad commented Sep 5, 2016

image

`

  <div ng-init="Init()">
    <table class="table">
      <tbody>
        <tr class="danger">
          <th>City</th>
          <th>Country</th>
        </tr>
        <tr ng-repeat="c in Cities" ng-class-odd="'success'" ng-class-even="'warning'">
          <td>{{c.Name}}</td>
          <td>{{c.CountryName}}</td>
        </tr>
      </tbody>
    </table>
  </div>
` output **Underscore.js Not Required** ![image](https://cloud.githubusercontent.com/assets/919452/18241103/4326edd6-7371-11e6-8bb2-96b391723d72.png)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment