0

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?

1 Answer 1

1

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;
                });
        }
    });

Hope I understood your right

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>
Sign up to request clarification or add additional context in comments.

7 Comments

sorry, I've should've explained my question in a clearer way.. Please see my edit
@Jon so the problem is to write proper ctrl that will represent the data in the view?
I edited in what I have so far in my controller. Each Blog has many Posts
Sorry m8, I'm not sure if I understood your problem. You trying to fetch data dynamically? You do want to provide id?
Thanks, this was similar to what I tried before but now it's causing my app to freeze but I guess that's a different question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.