Click here to Skip to main content
15,905,508 members
Articles / Web Development / HTML
Tip/Trick

Sample Angularjs Application - Kids Alphabets Tutor

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
9 Jul 2015CPOL 11.4K   6   2
Developed Kids Alphabets Tutor using ng-repeat, ng-bind, ng-hide, setTimeout in AngularJS

Introduction

This code displays the content in an array collection with a certain time delay. The tip helps newbies in AngularJs. The code demonstrates the usage of ng-repeat, ng-bind, ng-hide. The array is stored in $scope.letters and the setTimeout in the $scope.testname function sets some delay before the div binds the data to itself.

Background

Using AngularJs, we can build the rich client UI. This is one of the mostly used Single page applications in the current web application development.

Using the Code

In the array $scope.letters, we intialized the alphabets to display.

HTML
$scope.letters = ['A-Apple', 'B-Ball', 'C-Cat', 'D-Dog', 
'E-Elephant', 'F-Frog', 'G-Girafee', 'H-Horse'];

setTimeout function will set the delay time using the <a href="http://www.dotnetselflearning.blogspot.in/p/blog-page.html" target="_blank">$scope.$apply()</a> in it and finally the value is returned by the $scope.testname to the ng-bind.

HTML
<div ng-bind="testname(letter)"> </div>
JavaScript
$scope.testname = function (letter) {
                setTimeout(function () {
                    $scope.test = letter;
                    $scope.$apply();
                }, time);
                time = time + 2000;
                return letter;
            }

Below is the final piece of code to achieve the task.

HTML
<html>
<head>
    <title>Angular.js Example</title>
    <script src="Scripts/angular.js"></script>
    <script>
        var nameApp = angular.module('NameApp', []);
        nameApp.controller('NameCtrl', function ($scope) {
            // I initialized only till alphabet 'H'.. Add more if you are topper in LKG.
            $scope.letters = ['A-Apple', 'B-Ball', 'C-Cat', 
            'D-Dog', 'E-Elephant', 'F-Frog', 'G-Girafee', 'H-Horse'];
            var time = 0;
            $scope.testname = function (letter) {
                setTimeout(function () {
                    $scope.test = letter;
                    $scope.$apply();
                }, time);
                time = time + 2000;
                return letter;
            }
        });
    </script>
</head>
<body ng-app="NameApp" ng-controller="NameCtrl">
    <center>
        <h1>Kids Alphabet Tutor</h1><br />
        <div ng-hide="letter" ng-repeat="letter in letters">
            <div ng-bind="testname(letter)"> </div>
        </div>
        <h1>
            {{test}}
            <img src="Images/{{test}}.PNG" 
            width="75px" height="75px" />
        </h1>
    </center>
</body>
</html>

Points of Interest

  • Image file Name should be the array element names initialized in the $scope.letters.
  • $scope.$apply() is a must and should be there to work setTimeout.

How It Looks

Reference link: http://dotnetselflearning.blogspot.in/2015/06/using-settimeout-function-in-angularjs.html

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionWhy not $timeout? Pin
hoangcute9x9-Jul-15 15:50
professionalhoangcute9x9-Jul-15 15:50 
Why don't you use the $timeout wrapper in AngularJS instead of setTimeout and $scope.$apply()? Big Grin | :-D
AnswerRe: Why not $timeout? Pin
User 1170753910-Jul-15 1:13
professionalUser 1170753910-Jul-15 1:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.