i am learning angular by doing small exercises. i have successfully displayed objects in controller and trying to learn to use service and factory. i am unsuccessful in displaying array "names" on a table using service. please look at my code to see what i am doing wrong and also how would use factory to display the same array. here is my code
edited question after receiving partial answer:
factory is not not displaying the information ethnicity .
var myApp = angular.module("app",[]);
myApp.filter("agefilter", function(){
var x= function(age){
if(age==23||age==22){
return "Young";
}
if(age==25){
return "Mature";
}
if(age==26){
return "Oldest";
}
}
return x;
});
myApp.controller("ctrl", function($scope,factoryy){
$scope.person=[
{name:"jay",age:25,salary:85000},
{name:"anu",age:23,salary:65000},
{name:"jose",age:26,salary:75000},
{name:"sharon",age:22,salary:70000}
];
// $scope.callnames = servicee.names();
$scope.calleth = factoryy.geteth();
});
myApp.factory("factoryy",function(){
var ethnicity = ["Asian-Indian","Asian-Indian","Chinese","Latino","American"];
return{
geteth: function(){
return ethnicity;
}
}
});
// myApp.service("servicee",function(){
// var names= ["jay","anu","sharon","jose","mary"];
// this.names = function(){
// return names;
// }
// });
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<title></title>
</head>
<body ng-app="app" ng-controller="ctrl" >
<table border="1">
<tr>
<th>Name</th>
<th>Age</th>
<th>Salary</th>
</tr>
<tr ng-repeat="i in person">
<td>{{i.name |uppercase }}</td>
<td>{{i.age|agefilter}}</td>
<td>{{i.salary|currency}}</td>
</tr>
</table><br><br><br>
<!--<table border="1">
<tr><th>Name</th></tr>
<tr ng-repeat="s in callnames">
<td>{{s}}</td>
</tr>
</table>-->
<table border="1">
<tr>
<th>Ethnicity</th>
</tr>
<tr ng-repeat="e in calleth">
<td>{{e}}</td>
</tr>
</table>
<script src="../js/angu.js"></script>
</body>
</html>