2

I am new to Angular.JS & are currently working on my first project. I have a page full of entries of the kind

<h1>Title</h1>
<p>Teaser</p>
<p>(a link)</p>
The teaser may include HTML formating or elements like the "&". What I want to do is to "parse" this Teaser so that the formating is "understood" properly by the browser (currently Google Chrome).

The user can click on the link and get to the respective detail page. Here, I already head the same problem, which I could solve by using the ng-bind-html directive as follows:

	$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;
	      });
	  }
	}
<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>

For the detail page, an id is passed to the shownews, but the first call is without an id to read all the news from where, the user can reach the detail page by each news link,

I tried to find out how I can use an index in an ng-repeat loop, but couldn't find an answer. Is it possible to find out in HTML how many times the loops executed each time? What I need is something like (in pseudocode):

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

or a similar additional tag so that I can use this in a special "myHTMLall".

Thank you for your answer!

EDIT: I modified the code now, but it still does not format the text in the main page with all news:

<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>

2
  • 2
    ng-repeat="news in allnews track by $index" Commented May 27, 2015 at 11:04
  • Thank you! That's what I am looking for! But what should I include so that track is no more an unexpected token? By the way: The solution below using the $index without the track works fine! Commented May 27, 2015 at 11:16

1 Answer 1

5

If i'm understanding your question correctly you are looking for AngularJS' built-in $index as described in the documentation for ngRepeat here:

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

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you! That is also a good answer for me! I can get now with
I can get now with <p>{{myHTML[$index]}}</p> the required content, but ng-bind-html="myHTML" is not running as expected: The content, which is formatted in the detail page by this exact command, is appearing without being parsed, such as "Golfing &amp; sports day". What I need is to get rid of such ampersands.
Did you make sure $sanitize is available?
I am sure, as first of all, it works in the details page, and there is also an error message when I press F12 that reads: TypeError: value.replace is not a function at decodeEntities (angular-sanitize.js:324) at htmlParser (angular-sanitize.js:241) at $sanitize (angular-sanitize.js:119) at Object.ngBindHtmlWatchAction [as fn] (angular-sanitize.js:420) at Object.Scope.$digest (angular.js:7790) at Object.Scope.$apply (angular.js:7991) at done (angular.js:9001) at completeRequest (angular.js:9141) at XMLHttpRequest.xhr.onreadystatechange (angular.js:9111)
Ok. Well I'm not sure if I can help you with this. I think your initial question actually contains 2 questions, of which one now has been answered? Maybe you will have more luck if you split them and make a new question with a fitting title for your currently remaining problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.