简体   繁体   English

Angular JS ng-repeat不起作用

[英]Angular js ng-repeat not working

I have done a sample app with angular js and added a ng-repeat to loop my data but it's not looping it. 我已经用angular js完成了一个示例应用程序,并添加了ng-repeat来循环我的数据,但它没有循环。 I have shown my code below 我在下面显示了我的代码

 <li style="background: white url('{{book.imgUrl}}') no-repeat" ng-repeat="book in books">
          <div>
            <h3>{{book.name}}</h3>
            <p>{{book.price}}</p>
            <ul>
              <li>Rating: {{book.rating}}</li>
              <li>Binding: {{book.binding}}</li>
              <li>Publisher: {{book.publisher}}</li>
              <li>Released: {{book.releaseDate}}</li>
            </ul>
            <p>{{book.details}}</p>
            <button class="btn btn-info pull-right" ng-click="addToKart(book)">Add to Kart</button>
          </div>
        </li>

I have created a live demo of the problem here 我已经在这里创建了问题的现场演示

In order to get value in view from controller , you have to bind that with $scope. 为了从controller的视图中获取值,您必须将其与$ scope绑定。

Your books variable isn't bind with scope 您的book变量未与作用域绑定

Convert this 转换这个

var books=[

to this 对此

$scope.books=[

or like this 或像这样

$scope.books=books

DEMO

Try this 尝试这个

Use $scope.books instead of var books = []; 使用$scope.books代替var books = [];

In your controller, instead of using 在您的控制器中,而不是使用

var books

use 采用

$scope.books

You are close. 你近了

Instead of var book you need to glue book variable with $scope 代替var book您需要使用$ scope粘合book变量

Like 喜欢

 $scope.book=[{

   // Rest of code
}]

Also first pass the dependency as string else you may see error during minification 还要先将依赖项作为字符串传递,否则在缩小过程中可能会看到错误

    app.controller('BookListCtrl',["$scope", function($scope) {
   // Rest of code

}])

您应该使用$scope.books而不是var books。

DEMO

Assign your book in specific controller scope object parameter as per below 根据以下内容在特定的控制器范围对象参数中分配您的书

app.controller('BookListCtrl', function($scope) {
$scope.books=books;
})

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

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