javascript - ng-Directive 在 ng-Controller 之前被调用以将数据绑定(bind)到 HTML

标签 javascript angularjs

这是我在 Stack 中的第一篇文章,我是 Angular 的新手。我正在努力生成动态跨度标签,而不是将它们放在 HTML 中。当我有静态 HTML 代码时它工作正常,但当它从 Angular 调用时它不起作用。因为 ng-Directive 在 ng-Controller 之前被调用。

这是静态HTML代码

<scroller height="150" width="200" padding="5" ng-model="test">

<span><a href="http://www.google.com" target="_blank"><img src="images/img1.png"/></a><p class="title">img0</p></span>
<span><a href="http://www.google.com" target="_blank"><img src="images/img1.png"/></a><p class="title">img1</p></span>
<span><a href="http://www.google.com" target="_blank"><img src="images/img1.png"/></a><p class="title">img2</p></span>
</scroller>

我想做的是以下方式。

<scroller height="150" width="200" padding="5" ng-model="test">
    <div ng-repeat="image in imagesArray">

        <span><a href="http://www.google.com" target="_blank"><img ng-src='{{image.img}}'/></a><p class="imgtitle">{{image.title}}</p></span>

      </div>
      </scroller>

我的 Controller 代码

function myCtrl($scope,$http){
 $scope.test = 0;  
 $http.get("data.json")
  .then(function(response) {
    var data = response.data;
    $scope.imagesArray = response.data.imagesArray || [];
    console.log($scope.imagesArray);
  });

对象将通过以下方式创建。

[Object, Object, Object, Object, Object, Object, Object]
0: Object
img: "images/img1.png"
title: "img1 "

等..

我还需要做其他事情吗?任何想法将不胜感激。

最佳答案

You need to repeat span elements. Current code will repeat div elements which will create multiple block level elements which is not the expected output.

ngRepeat directive instantiates a template once per item from a collection

试试这个:

<scroller height="150" width="200" padding="5" ng-model="test">
  <span ng-repeat="image in imagesArray"><a href="http://www.google.com" target="_blank"><img ng-src='{{image.img}}'/></a><p class="imgtitle">{{image.title}}</p></span>
</scroller>

关于javascript - ng-Directive 在 ng-Controller 之前被调用以将数据绑定(bind)到 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35402554/

相关文章:

javascript - Angular 大表在搜索时卡住网页

javascript - Angular js 没有在 Controller 上返回任何内容

angularjs - $http.get 在 ionic 框架(angularjs)中有 2 个参数

javascript - 删除不需要的 '-' 字符

javascript - 无法在html中显示json数据

javascript - 背包示例 "with-typescript"无法编译

angularjs - disabled=false 在 typescript 2.1 中不起作用

Javascript 变量未定义

javascript - vue js 中组件加载时默认点击按钮

javascript - $scope.$watch 没有在指令中被触发