<!DOCTYPE html><html lang="en" ng-app="myApp"><head> <meta charset="UTF-8"> <script src="js/angular.js"></script> <title>Title</title></head><body><div ng-controller="myCtrl"> <div> <img ng-repeat="obj in pic track by $index " ng-src="img/{{obj}}" > //此时track by $index 告诉应用按照下表循环 </div> <button ng-click="loadMore()">加载更多</button></div><script> var app = angular.module(‘myApp‘, [‘ng‘]); app.controller(‘myCtrl‘, function ($scope) { $scope.pic=[‘1.jpg‘] $scope.loadMore=function(){ var loadP= Math.ceil(Math.random()*3)+".jpg"; //会重复出现123.jpg,把他添加到数组中会有重复的数据,循环时会报错 $scope.pic.push(loadP) console.log($scope.pic) } })</script></body></html>
时间: 2024-12-10 07:53:09