2014-06-20 3 views
0

내가 AngularJS와에 새로 온 사람과 아약스 요청 후 컨트롤러 변수는 내가업데이트

var articles = angular.module('Articles', []).config(function($interpolateProvider){ 
     $interpolateProvider.startSymbol('{§').endSymbol('§}'); 
    }); 

    articles.controller('CommentsCtrl', ['$scope', '$http', function($scope, $http) {   
    this.comments = []; 

    this.showComments = function (index) {      
     $http({ 
      method: 'POST', 
      url: '{{ path ('backend_article_commentaires')}}', 
      data: {articleID:index} 
      }) 
      .success(function (data, status, headers, config) { 
       comments = data; 
      }) 
      .error(function (data, status, headers, config) { 
      console.log(data) 
      }); 
      } 
     }]); 

문제가 ... 내 코드 파크 무엇을 잘못 TI 그림을 시도하는 시간을 보냈다 그 의견 변수를 변경하지 마십시오 ...

제발 어떻게 해결할 수 있습니까?

업데이트 :

<div class="md-modal md-effect-13" id="modal-comments" 
    ng-controller="CommentsCtrl as ctrl"> 
    <div class="md-content"> 
     <h3> 
      <i class="fa fa-comments"></i> Tous les commentaires 
     </h3> 
     <div> 
      <div id="list-comment"> 
       <h4>Cet article n'a aucun commentaires...</h4> 
       <li ng-repeat="comment in ctrl.comments" class="media"> 
        <a>{{ comment.title }}</a> 
       </li> 
      </div> 
      <div class="button-row"> 
       <button class="btn btn-danger md-close md-yes">Fermer</button> 
      </div> 
     </div> 
    </div> 
</div> 
+0

당신은 내가 노력 –

+0

을 this.comments의 = 데이터를해야 내가 성공 기능에서 사용할 수있는 "이"그런 식으로 유지하기 위해 rootThis 변수를 생성하지만, 아무튼 't work :/ – KimiBst

+0

너는 그렇지 않다고하니? devtools로 범위를 탐색하고 있습니까? 아니면 데이터가 페이지에 표시되지 않기 때문입니까? –

답변

0

귀하의 문제는 범위 (그리고 각도 범위) 중 하나는이 당신의 성공 함수에서 컨트롤러에이 같은 동일하지 않을 것입니다. 컨트롤러의 변수에 "this"를 지정한 다음 참조해야합니다. 당신이 볼 수 있듯이

articles.controller('CommentsCtrl', ['$scope', '$http', function($scope, $http) {   
    this.comments = []; 

    var rootThis = this; 

    this.showComments = function (index) {      
    $http({ 
      method: 'POST', 
      url: '{{ path ('backend_article_commentaires')}}', 
      data: {articleID:index} 
     }) 
     .success(function (data, status, headers, config) { 
      rootThis.comments = data; 
     }) 
     .error(function (data, status, headers, config) { 
      console.log(data) 
     }); 
    } 
}]); 

나는

+0

나는 이것을 시도했지만 작동하지 않는 것 같습니다. 나는 할 수 없습니다. 그 경고 (rootThis.comments) – KimiBst

+0

jsFiddle을 만들 수 있습니다 –

+0

귀하의 URL은 매우 꺼져 보입니다. 왜 괄호 안에 들어 있습니까? –