Hi, I’m really new in the IONIC world, so, last day I did an example of an app that take json data provide by my Rails app and it works fine. The problem is that Im working with another example that I found, but this works not only with controllers so also with services.
In the services.js file of the example, it uses an object stored in a var, hardcoded in the same file, instead of that object I want to use my own json object (like in the first example that work fine).
So, this is the controller that works perfectly (in my first app):
(function(){
var app = angular.module (‘flightsController’,[]);
app.controller(‘FlightsController’,function($scope, $http){
var url = ‘http://192.168.1.10:3000/api/flights’;
$http.get(url)
.success(function(flights){
$scope.flights = flights;
})
.error(function(data) {
console.log(‘server side error occurred.’)
});
});
})();
And this is the service file (in the second app):
angular.module(‘directory.services’, [])
.factory('FlightService', function($q) {
var flights = [
{"id": 1, "firstName": "Caracas", "lastName": "Valencia", "managerId": 0, "managerName": "", "reports": 4, "title": "---", "department": "Corporate", "cellPhone": "617-000-0001", "officePhone": "781-000-0001", "email": "jking@fakemail.com", "city": "Boston, MA", "pic": "James_King.jpg", "twitterId": "@fakejking", "blog": "http://coenraets.org"},
{"id": 2, "firstName": "Valencia", "lastName": "Maracaibo", "managerId": 1, "managerName": "James King", "reports": 2, "title": "---", "department": "Marketing", "cellPhone": "617-000-0002", "officePhone": "781-000-0002", "email": "jtaylor@fakemail.com", "city": "Boston, MA", "pic": "Julie_Taylor.jpg", "twitterId": "@fakejtaylor", "blog": "http://coenraets.org"},
{"id": 3, "firstName": "Maracaibo", "lastName": "Merida", "managerId": 1, "managerName": "James King", "reports": 0, "title": "---", "department": "Accounting", "cellPhone": "617-000-0003", "officePhone": "781-000-0003", "email": "elee@fakemail.com", "city": "Boston, MA", "pic": "Eugene_Lee.jpg", "twitterId": "@fakeelee", "blog": "http://coenraets.org"},
];
// We use promises to make this api asynchronous. This is clearly not necessary when using in-memory data
// but it makes this service more flexible and plug-and-play. For example, you can now easily replace this
// service with a JSON service that gets its data from a remote server without having to changes anything
// in the modules invoking the data service since the api is already async.
return {
findAll: function() {
var deferred = $q.defer();
deferred.resolve(flights);
return deferred.promise;
},
findById: function(flightId) {
var deferred = $q.defer();
var flight = flights[flightId - 1];
deferred.resolve(flight);
return deferred.promise;
},
findByName: function(searchKey) {
var deferred = $q.defer();
var results = flights.filter(function(element) {
var fullName = element.firstName + " " + element.lastName;
return fullName.toLowerCase().indexOf(searchKey.toLowerCase()) > -1;
});
deferred.resolve(results);
return deferred.promise;
},
findByManager: function (managerId) {
var deferred = $q.defer(),
results = flights.filter(function (element) {
return parseInt(managerId) === element.managerId;
});
deferred.resolve(results);
return deferred.promise;
}
}
});
instead on that flights object hardcoded in services, I just want to take the .json object like in the first code, I’ve tried many things but I can’t figure out how to do it.
I think is not that hard, but for real i can’t figure it out