Reputation: 2280
I have this in one of my controllers (notice the id):
Post.get
id: "2"
# Success
, (response) ->
$scope.post = response
# Error
, (response) ->
But what I'd like to do is some how dynamically get a specific post i.e. {{getPost("2")}}
Sorry if what I'm asking is pretty obvious but I'm a beginner and have been stuck on this for hours. Could someone please shed some light?
Upvotes: 0
Views: 44
Reputation: 558
it's better practice to separate server request into factory, for example lets call your factory jonFactory and your App jonApp so now we have services.js file that includes all our factories/services.
angular.module('jonApp',[]).
factory('jonFactory', function($http) {
function.getPost = function(id) {
var url = "your url for post";
var args = { 'id': id };
$http.post(url, args)
.success(function(data){
return data;
});
}
});
Your Ctrl should use your factory, so you have to include it through:
angular.module('jonApp.controllers', []).
controller('JonController', function($scope, jonFactory) {
$scope.getPostById = function(id) {
return jonFactory.getPost(id);
}
});
and in the view just display the function result:
<div>{{getPostById(2)}}</div>
Upvotes: 1