2014-01-08 3 views
-1

AngularJS 앱 시드를 사용하려고하는데 이상한 결과가 나타납니다. view2.html에서AngularJS의 컨트롤러

나는이 :

<div ng-controller="view_ctrl_2"> 
    <div id="view2"> 
     <table class="table table-striped"> 
      <thead> 
       <tr> 
        <td>col1</td> <td>col2</td> <td>col3</td> <td>col4</td> 
       </tr> 
      </thead> 
      <tr ng-repeat="entry in entries"> 
       <td>{{entry.col1}}</td> <td>{{entry.col2}}</td> <td>{{entry.col3}}</td> <td>{{entry.col4}}</td> 
      </tr> 
     </table> 
    </div> 
</div> 

내 controllers.js에

내가 그러나

angular.module('myApp.controllers', []) 
    .controller('view_ctrl_2', [function() { 
     var entries = [ 
       { "col1":"val01", "col2":"val02", "col3":"val03", "col4":"val04" }, 
       { "col1":"val05", "col2":"val06", "col3":"val07", "col4":"val08" }, 
       { "col1":"val09", "col2":"val10", "col3":"val11", "col4":"val12" } 
     ]; 
    }]); 

이 파일을, 나는 페이지의 출력을받지 못했습니다. 그것은 테이블을 보여 주지만, ng-repeat는 그것에 테이블을 놓지 않습니다. 내 생각 엔 $ scope 변수와 관련이 없다는 것입니까?

+1

대신 $ scope.entries가 필요합니다. $ 범위 변수 만 컨트롤러 외부에서 볼 수 있습니다. –

답변

5

당신은 $ 범위에 entries을 추가해야

angular.module('myApp.controllers', []) 
    .controller('view_ctrl_2', ['$scope', function($scope) { 
     $scope.entries = [ 
       { "col1":"val01", "col2":"val02", "col3":"val03", "col4":"val04" }, 
       { "col1":"val05", "col2":"val06", "col3":"val07", "col4":"val08" }, 
       { "col1":"val09", "col2":"val10", "col3":"val11", "col4":"val12" } 
     ]; 
    }]); 

, $scope가 더 정교한 컨트롤러 ['$scope', function($scope)...var entries=

의 대신 $scope.entries=의 사용에 주입되고 있음을 모든 종속성을 유의하시기 바랍니다 제어기가 주입 될 필요가있다. http 호출을하고 약속을 사용하는 경우, var 항목 대신

.controller('view_ctrl_2', ['$scope', '$q', '$http', function($scope, $q, $http) 
+0

흥미 롭습니다. 스코프가 맞는지 아닌지에 대해 논쟁을 벌였습니다. 그런 식으로 전달할 필요가 있다는 것을 깨닫지 못했습니다. 나는 그것이 가능한 한 빨리 받아 들일 것이다. – Jacobm001

+0

예, 서비스를 포함한 모든 종속성은 $ scope, $ rootScope, $ q, $ http 또는 다른 어떤 것과 유사하게 주입됩니다. – lucuma

관련 문제