简体   繁体   English

Ng-repeat不适用于AngularJs

[英]Ng-repeat doesn't work AngularJs

New at Angular, I spent 2 hours searching my error, helped by a few docs and watching the already existing posts but nothing to do.. 我是Angular的新员工,在几个文档的帮助下,我花了2个小时来搜索错误,并观看了已经存在的帖子,但无事可做。

I'm just hard declaring an object array and try to loop through it: 我只是在声明一个对象数组并尝试遍历它:

Code: 码:

angular.module('MyAppModule', [ ])

.controller('GreetsController', ['$scope', function ($scope) {
  $scope.name = prompt('What\'s your name ?');
}])

.controller('ListController', ['$scope', function ($scope) {
  $scope.personNb = this.persons.length;

  $scope.persons = [
    {
      image: 'images/images(1).jpg',
      name: 'John Doe',
      age: 23
    },
    {
      image: 'images/images.jpg',
      name: 'John Doe',
      age: 23
    },
    {
      image: 'images/téléchargement.jpg',
      name: 'John Doe',
      age: 23
    },
    {
      image: 'images/téléchargement(1).jpg',
      name: 'John Doe',
      age: 23
    }
  ];
}]);

Html: HTML:

<div ng-controller="GreetsController">
  <h1>Coding with AngularJs</h1>
  <h2 ng-show="name">{{"Welcome " + name + " !"}}</h2>
</div>

<div ng-controller="ListController" ng-repeat="person in persons">
  <h3>{{person.name}}</h3>
  <h3>{{person.age}}</h3>
</div>

{{ListController.persons[0].age}}
<h3 ng-show="{{ListController.person_nb}}">There is a total of {{ListController.person_nb}} register</h3>

I didn't captured it but all the scripts are includes and I add 'MyAppModule' in the depedencies array of app.js 我没有捕获它,但是所有脚本都包含在内,并且在app.js的依赖数组中添加了“ MyAppModule”

You have 3 mistakes in your HTML: 您的HTML中有3个错误

  1. You must declare your ng-app before calling your controller(s), then put this in one tag above: 您必须在调用控制器之前声明ng-app ,然后将其放在上面的一个标签中:

So, your HTML becomes this: 因此,您的HTML变为:

<div ng-app="MyAppModule">
  <div ng-controller="GreetsController">
    <h1>Coding with AngularJs</h1>
    <h2 ng-show="name">{{"Welcome " + name + " !"}}</h2>
  </div>

  <div ng-controller="ListController" ng-repeat="person in persons">
    <h3>{{person.name}}</h3>
    <h3>{{person.age}}</h3>
  </div>

  {{ListController.persons[0].age}}
  <h3 ng-show="{{ListController.person_nb}}">There is a total of {{ListController.person_nb}} register</h3>
</div>
  1. You must put these lines: 您必须放置以下行:
{{ListController.persons[0].age}}
<h3 ng-show="{{ListController.person_nb}}">There is a total of {{ListController.person_nb}} register</h3>

inside the <div> tag that is declared your ng-controller . 在声明为ng-controller<div>标记内。

  1. You you should only call this way (without the ListController ) and also ng-show works without interpolation {{}} : 您只应使用这种方式(不使用ListController )进行调用,并且ng-show 无需插值{{}}
{{persons[0].age}}
<h3 ng-show="person_nb">There is a total of {{person_nb}} register</h3>

I recommend you to take a look on this tutorial and follow it step by step. 我建议您看一下本教程 ,并逐步进行学习。

Also, check this complete demo with your code, made by @Chev: 此外,请使用@Chev制作的代码检查此完整的演示:

DEMO 演示

You had two errors: 您有两个错误:

  1. <div ng-controller="ListController" ng-repeat="person in persons"> Break the ng-controller and ng-repeat into different elements. <div ng-controller="ListController" ng-repeat="person in persons">将ng-controller和ng-repeat分为不同的元素。 You don't want to repeat the controller. 您不想重复控制器。
  2. $scope.personNb = this.persons.length; this.persons does not exist. this.persons不存在。
  3. The summary at the bottom you are trying to access the controller by its class name ListController . 您试图在底部的摘要中通过类名称ListController访问控制器。

See the changes I made. 查看我所做的更改。 click Run code snippet for a demo. 点击Run code snippet以进行演示”。

 angular.module('MyAppModule', [ ]) .controller('GreetsController', ['$scope', function ($scope) { $scope.name = ""; $scope.name = prompt('What\\'s your name ?'); }]) .controller('ListController', ['$scope', function ($scope) { $scope.persons = [ { image: 'images/images(1).jpg', name: 'John Doe', age: 23 }, { image: 'images/images.jpg', name: 'John Doe', age: 23 }, { image: 'images/téléchargement.jpg', name: 'John Doe', age: 23 }, { image: 'images/téléchargement(1).jpg', name: 'John Doe', age: 23 } ]; $scope.personNb = $scope.persons.length; }]); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="MyAppModule"> <div ng-controller="GreetsController"> <h1>Coding with AngularJs</h1> <h2 ng-show="name">Welcome {{name}}!</h2> </div> <div ng-controller="ListController"> <div ng-repeat="person in persons"> <h3>{{person.name}}</h3> <h3>{{person.age}}</h3> </div> {{persons[0].age}} <h3 ng-show="{{personNb}}">There is a total of {{personNb}} register</h3> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM