我想使用ng-repat重复div标签

时间:2016-12-30 09:57:52

标签: html angularjs

<script>
		var app = angular.module("myapp", []); 
     app.controller('test',function($scope){
		 $scope.my="hi";
	  $scope.myimag=["Feature-Category1.png","Feature-Category2.png","Feature-Category3.png","Feature-Category1.png","Feature-Category1.png"];

			 });
		</script>
<body ng-app="myapp">
<div ng-controller="test">
<div class="col-md-3 col-sm-3 col-xs-3" ng-repeat="x in myimag">
<a href="#"><img alt="" ng-src="{{x}}"></a>
	</div> 
  </div>
	</body>										

我想重复myimg中的项目,但是它不能正常运行

2 个答案:

答案 0 :(得分:0)

您缺少模块,控制器并再次使用 $scope 变量而不是var,

  $scope.myimg=["img.png","img1.png","img2.png"];

<强>样本

&#13;
&#13;
var testApp= angular.module('test',[])
angular.module('test').controller('testCtrl',function($scope){
$scope.myimg=["altair7.jpg","altair6.jpg"];
})
&#13;
<!doctype html>
<html>

<head>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
  <script src="app.js"></script>
  <script src="testCtrl.js"></script>
</head>

<body ng-app="test">
  <div ng-controller="testCtrl">
   <div class="col-md-2" ng-repeat="x in myimg ">
      <a href="#"><img ng-src="{{x}}"></a>
    </div>
  </div>
</body>

</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

代码存在三个问题:

  1. 您尚未定义角度模块。
  2. 您还没有为该视图定义控制器。
  3. 为了实现双向数据绑定,您需要使用 $ scope 定义变量,而不是将其定义为&#39; var&#39;
  4. 更新您的代码,如下所示:

    angular.module('myApp', [])
    .controller('myController', function TodoCtrl($scope) {
        $scope.myimg=["img.png","img1.png","img2.png"];
    });
    

    <强> HTML

    <div ng-app="myApp" ng-controller="myController">
      <div class="col-md-2" ng-repeat="x in myimg ">
        <a href="#"><img ng-src="{{x}}"></a>
       </div>
    </div>