Angular Arrays未输出到屏幕

时间:2016-06-25 12:20:39

标签: angularjs

我创建了一个名为reveiws的对象,我试图让它出现在屏幕上。当我在评论中提出ng-repeat =&#34; rev&#34;在div id中,屏幕上根本没有显示任何内容。除了Hello之外,它是空白的。此外,没有控制台错误。如果我删除该行ng-repeat="rev in reviews"并将其保留为<div id="reviews" ng-controller="mainController as ctrl"> 然后它显示为

Hello
{{rev.name}} {{"HI"}}

因此代码仅在控制器中受到影响,因为Hello是在控制器外部正确输出的。为什么数组没有输出名称?

的index.html

<html ng-app="objectTest" xmlns="http://www.w3.org/1999/xhtml">
<head>

  <title></title>
</head>

<body>
  {{"Hello"}}

  <div id="reviews" ng-controller="mainController as ctrl" ng-repeat="rev in reviews">
    {{rev.name}} {{"HI"}}
  </div><script src="bower_components/angular/angular.js" type="text/javascript">
</script><script src="app.js" type="text/javascript">
</script>
</body>
</html>

app.js

    var app = angular.module('objectTest', [])
    .controller('mainController'['$scope' , function($scope){
    $scope.reviews = [
    {
    name: "Joe Smith",
    description: "It works!"

    },
{
  name: "Bill Smith",
 description: "great site"
}
    ];


    }]);

1 个答案:

答案 0 :(得分:1)

如果在视图中定义控制器时删除“as”语法,它将起作用。

ng-controller="mainController"更改为var app = angular.module('objectTest', []) .controller('mainController', ['$scope' , function($scope){ $scope.reviews = [ { name: "Joe Smith", description: "It works!" }, { name: "Bill Smith", description: "great site" } ]; }]);

已更新:已添加工作代码

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="objectTest" ng-controller="mainController">
  <div ng-repeat="rev in reviews">
    {{ rev.name }}
  </div>
</div>
pthread_cond_signal()