+-+-+ +-+ +-+-+-+-+
|G|O| |4| |H|I|V|E|
+-+-+ +-+ +-+-+-+-+

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

How to repeat HTML elements in AngularJS

BY: @coderlovely | CREATED: March 11, 2018, 3:03 p.m. | VOTES: 12 | PAYOUT: $0.12 | [ VOTE ]

What Will I Learn?

A.How to repeat HTML elements in AngularJS
B.We want to display all tracks with the duration as a bulleted list
C.Moreover we want to display the index and the information if the track is the first, in the middle or the last in the list

Requirements

Basic Knowledge of HTML and CSS language .
Basic Knowledge of JavaScript language .
Has an editor text support HTML and JavaScript .

Difficulty
Basic

In this tutorial I would like to show you how to repeat HTML elements in AngularJs. So let’s assume we have a list of album tracks. We want to display all tracks with the duration as a bulleted list. Moreover we want to display the index and the information if the track is the first, in the middle or the last in the list:

Coldplay

  1. Paradise 4:21 (first track)
  2. Princes of China 3:35 (middle track)
  3. The Scientist 4:26 (middle track)
  4. Atlas 3:22 (last track)

Here’s the source code:












        {{$index + 1}}. {{track.name}} {{track.duration}} ({{$first ? 'first' : $middle ? 'middle' : 'last'}} track)





var app = angular.module('myApp', []);
app.controller('AlbumCtrl', function($scope) {
    $scope.tracks = [
        {name:'Paradise', duration:'4:21'},
        {name:'Princess of China', duration:'3:35'},
        {name:'The Scientist', duration:'4:26'},
        {name:'Atlas', duration:'3:22'}
    ];
});

As you can see you can use the data-ng-repeat attribute directive to loop over a collection in the model (tracks) and repeat the < li > element.

Inside the loop you can use implicit properties llike $index, $first, $middle and $last which give you the current loop index (starts at 0) and booleans if the element is the first, in the middle or the last in the list respectively.

The cool thing is that you not only can loop over a list but also the key-value pairs in an object. So let’s say the album tracks are stored in a JavaScript object:


Coldplay




        {{$index + 1}}. {{name}} {{duration}} ({{$first ? 'first' : $middle ? 'middle' : 'last'}} track)


var app = angular.module('myApp', []);
app.controller('AlbumCtrl', function($scope) {
    $scope.tracks = {
        'Paradise' : '4:21',
        'Princess of China' : '3:35',
        'The Scientist' : '4:26',
        'Atlas' : '3:22'
    }
});

[IMAGE: https://i.hizliresim.com/Md4pm6.png]

Posted on Utopian.io - Rewarding Open Source Contributors

TAGS: [ #utopian-io ] [ #utopian ] [ #utopianio ]

Replies

@portugalcoin | March 11, 2018, 10:09 p.m. | Votes: 0 | [ VOTE ]

Deleted!

@utopian-io | March 12, 2018, 10:52 a.m. | Votes: 1 | [ VOTE ]

Hey @coderlovely, your contribution was rejected by the supervisor @deathwing because he found out that it did not follow the Utopian rules.

Upvote this comment to help Utopian grow its power and help other Open Source contributions like this one. Do you want to chat? Join me on Discord.

@deathwing | March 12, 2018, 1:49 p.m. | Votes: 1 | [ VOTE ]

Your contribution cannot be approved because it does not follow the Utopian Rules.

After secondary review, this tutorial has been rejected due to it being too trivial.

You can contact us on Discord.
[utopian-moderator]

@coderlovely | March 12, 2018, 8:28 p.m. | Votes: 0 | [ VOTE ]

are you kidding me...give me the right...i will complain you

[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>