Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
28
rated 0 times [  34] [ 6]  / answers: 1 / hits: 15263  / 9 Years ago, wed, august 19, 2015, 12:00:00

I would like to know the way for generating random id in angular template once the page loads. The problem I am facing is that every second I get different ID in the two spans. I want it to be the same on both places and dont want it to change its value every second. Here is my simple template:



<main class=has-header has-padding>
<div id = command>-ID:<span>{{vm.getRandomId()}}</span> -connect -run</div>
<hr>
<div id = command>-ID:{{vm.getRandomId()}}</div>




my controller:



'use strict';

(function() {
angular
.module('myApp')
.controller('myController.ctrl', myController);

myController.$inject = ['$scope'];

function screenSharingCtrl($scope) {
angular.extend(this, {
getRandomId:getRandomId
});

function getRandomId() {
return Math.floor((Math.random()*6)+1);
}
}
})();


Sorry if it is a duplicate question.
Any help is appreciated!
Thank you!


More From » angularjs

 Answers
9

just change the binding to



{{ vm.id }}


and your viewmodel to



function screenSharingCtrl($scope) {

function getRandomId() {
return Math.floor((Math.random()*6)+1);
}

this.id = (typeof this.id === 'undefined') ? getRandomId() : this.id;
}

[#65368] Sunday, August 16, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryanulyssesb

Total Points: 91
Total Questions: 105
Total Answers: 102

Location: England
Member since Tue, Sep 8, 2020
4 Years ago
ryanulyssesb questions
Sat, Mar 20, 21, 00:00, 3 Years ago
Mon, Sep 14, 20, 00:00, 4 Years ago
Mon, Mar 9, 20, 00:00, 4 Years ago
Sun, Jul 7, 19, 00:00, 5 Years ago
;