Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I want to send data from api to my view... So I need to take data from get reqest and then send that data to my view on my page.

Here is my controller:

 angular.module('commentsApp',['notifications'])
           .config(function($routeProvider,$locationProvider) {
                $routeProvider.
                        when('/project/:id', {
                            controller: 'CommentsCtrl',
                            template: '<%comments%>'
                        }).
                        otherwise({
                            template: 'aaaaaaa',
                            redirectTo: '/'

                        });               

            })

          .controller('CommentsCtrl',function($scope,$http,$routeParams){
              $scope.comments = [];
              $scope.param = $routeParams.id;
              $http.get("/api/comments/"+ $scope.param)
              .success(function(data){
                   $scope.comments = data;       
              })
              .error(function(data){
                  console.log(data);
              });
          });

Here is my html:

<div id="comment" ng-app="commentsApp" class="panel-body">
                                <div ng-view>
                                    <h1><% ng %></h1> 
                                </div>


                            </div>
share|improve this question
    
You mean you want to display the comments in your scope? I'm not sure what the issue is; if they're in scope you should be able to repeat over them or do whatever you want with them. – Dave Newton Aug 6 '15 at 16:53

You should be declaring your ng-app in the <html> tag. I'm not sure what the <% ng %> is, nor the <% comments %> in your template, but that is not how angular renders data to the view. Since comments is an array, you'll want to use an ng-repeat to display the data.

Something like (in your template)

<ul>
  <li ng-repeat="comment in comments">
    {{comment}}
  </li>
</ul>

The {{comment}} is how angular renders variables, similar to erb's <%= comment %>. Comment, is a local variable for each <li> that gets generated by the ng-repeat, which gets its data from the $scope variable comments.

EDIT: Also you may need to $scope.$apply(); in your CommentsController after you assign $scope.comments = data. Not sure on this point, I haven't got angular's full digest cycle in my head yet.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.