Last active
September 5, 2016 09:51
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]="--"; | |
} | |
}); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
`