2014-03-29 4 views
0

내가 여기 문제는

<!-- ngRepeat: list in $parent.lists --> 

내 코드

입니다 참조 (내 NG-반복 포함)
var module = angular.module('app', ['ngRoute']); 

module.config(function($routeProvider) { 
    $routeProvider 
     .when('/:num', 
     { 
     templateUrl: "list.html", 
     controller: "ListController" 
     }).otherwise({redirectTo: '/'}); 
}); 

module.service('ListService', function ($http, $routeParams) { 

    this.getLists = function() { 
     return $http.get("testmysql.php", {params : {num : $routeParams.num}}) 
     .then(function(results){ 
      //Success; 
      console.log(results.data); 
      return results.data; 
     }, function(results){ 
      //error 
      console.log("Error: " + results.data + "; " 
            + results.status); 
      return results.data; 
     }); 
    } 
}); 

module.controller("ListController", function ($rootScope, $scope, $routeParams, ListService) { 
    $rootScope.$on('$routeChangeSuccess', function(event, current, previous){ 
     ListService.getLists().then(function(data){ 
      $scope.lists = data; 
     }); 
    }); 
}); 

index.html을 본문 ...

<body ng-app="app"> 
    <div ng-view></div> 
</body> 

그리고 list.html

<div id="container"> 
    <h1 id="websiteTitle">Website</h1> 
    <h2 id="slogan">The only site you'll ever need</h2> 
    <!-- View below contains unique id from URL --> 
    <div class="outerBox" ng-repeat="list in lists"> 
    <div class="innerBox"> 
     <h1 class="listTitle">{{list[0].listname}}</h1> 
     <div class="website" ng-repeat="site in list"> 
     <span class="index">{{$index + 1}}</span><a href = {{site.url}}><img src= "http://www.google.com/s2/favicons?domain={{site.url}}">{{site.name}}</a> 
     </div> 
     </div> 
    </div> 
</div> 

범위에 액세스 할 수없는 이유를 모르겠습니다. 나는 내 컨트롤러에서 $ rootscope.test = "test"를 시도했다. 그런 다음 {{test}}했을 때 list.html에서 작동했습니다. 그러나 ng-repeat에는 운이 없습니다.

또한 ""템플릿을 사용하고 list.html HTML을 색인 파일의 본문에 넣으면 모든 것이 완벽하게 작동하지만 2 개의보기가 필요하기 때문에 필요하지 않습니다.

해결 : $ rootScope. $ on이 내 문제였습니다. 나를 배신했습니다. 그것을 고치겠습니다.

+0

console.log (results.data)를 추가하십시오. 그 시점에서 데이터를 따라 가기 쉽습니다. 또한, $ routscope.on 리스너에 대한 이유가 있습니까? 실제로 발사되는거야? 특별한 이유가 없으면 아마 그것을 잃어 버릴 수 있고 문제를 일으킬 수도 있습니다. 그것은 어쨌든 경로가 변경 될 때까지 컨트롤러가 실행되지 않기 때문에 필요하지 않습니다. 아마도 당신 문제. – Lenny

+0

컨트롤러에서 정의 된 일부 기본 배열로 이것을 테스트 해 보셨습니까? – shaunhusain

+0

@Lenny console.log (results.data)는 내 배열 배열을 예상대로 기록합니다. 예, 그것은 발사, 모든 기능을 내가 index.html의 본문에 list.html 넣고, templateUrl template = ""로 변경하면. –

답변

0

경로 변경 수신기가 필요한 이유가 무엇인가요? 결코 발사되지 않을 가능성이 있습니다. 대신이 방법을 시도해보십시오.

module.controller("ListController", function ($rootScope, $scope, $routeParams, ListService) { 
    ListService.getLists().then(function(data){ 
     $scope.lists = data; 
    }); 
});