如何"知道" Angular.JS中循环中ng-repeat的索引?

时间:2015-05-27 11:00:27

标签: angularjs angularjs-ng-repeat

我是Angular.JS&的新手。目前正在开展我的第一个项目。我有一个页面,里面装满了那种



<h1>Title</h1>
<p>Teaser</p>
<p>(a link)</p>
&#13;
&#13;
&#13; 预告片可能包括HTML格式或类似&#34;&amp;&#34;等元素。我想要做的是&#34;解析&#34;这个预告片使得格式化得到了解,并且#34;通过浏览器(目前是谷歌浏览器)正确使用。

用户可以单击该链接并进入相应的详细信息页面。在这里,我已经解决了同样的问题,我可以使用ng-bind-html指令解决这个问题,如下所示:

&#13;
&#13;
	$scope.shownews = function(id) {

	  if (typeof id === 'undefined') {
	    $http({
	      method: 'GET',
	      url: 'http://dev.ivm.at/getallnews.php'
	    }).
	      success(function(data, status, headers, config) {
	        $scope.allnews = data;
	        $scope.myHTML = [];

	        for (var i = 0; i < $scope.allnews.length; i++) {
	          $scope.myHTML[i] = $scope.allnews[i].Teaser;
	        }
	      });
	  } else {
	    $http({
	      method: 'GET',
	      url: 'http://dev.ivm.at/getnews.php?ID=' + id
	    }).
	      success(function(data, status, headers, config) {
	        $scope.singlenews = data;
	        $scope.Newstitel = $scope.singlenews[0].Title
            //[...]
	        $scope.Inhalt = $scope.singlenews[0].Inhalt;
	        //[...]
	        $scope.myHTML = "<h1>" + $scope.Newstitel + "</h1><br>" + $scope.Inhalt;
	      });
	  }
	}
&#13;
<div data-role="page" id="einenews">
  <section class="beitrag" ng-bind-html="myHTML">
    <article>
      <h1>{{Newstitel}}</h1>
      <div class="beitrag-img">		   
        <a href="lighbox mit bild"></a>
        <img src="" + {{Bilderlink}}> 
      </div> 
      <p>{{Inhalt}}</p>
    </article>
  </section>		
</div>
&#13;
&#13;
&#13;

对于详细信息页面,会将一个id传递给shownews,但第一次调用没有id来读取所有新闻,用户可以通过每个新闻链接到达详细信息页面,

我试图找出如何在ng-repeat循环中使用索引,但无法找到答案。是否有可能在HTML中找到每次循环执行的次数?我需要的是(伪代码):

ng-repeat="news in allnews; count = count + 1;"

或类似的附加标签,以便我可以在特殊的&#34; myHTMLall&#34;中使用它。

感谢您的回答!

编辑:我现在修改了代码,但它仍然没有使用所有新闻格式化主页中的文字:

&#13;
&#13;
<div data-role="page" id="allnews">
  <div id="sub">
    <section class="blog">
      <article ng-bind-html="myHTML" ng-repeat="news in allnews">
        <h1>{{news.Title}}</h1>
        <!-- <p>{{news.Teaser}}</p> -->
        <p>{{myHTML[$index]}}</p>
        <p class="readmore">
          <a href="onenews" ng-click="shownews(news.ID)">
		              Weiterlesen: {{news.Title}}
          </a>
          <span class="readmore_icon">
	                           	</span>
        </p>
      </article>
    </section>
  </div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:5)

如果我正确理解您的问题,您正在寻找AngularJS的内置$index,如ngRepeat文档中所述:

https://docs.angularjs.org/api/ng/directive/ngRepeat