javascript - generate random id number within angular template just once -
i know way generating random id in angular template once page loads. problem facing every second different id in 2 spans. want same on both places , dont want change value every second. here 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 duplicate question. appreciated! thank you!
just change binding
{{ vm.id }}
and viewmodel to
function screensharingctrl($scope) { function getrandomid() { return math.floor((math.random()*6)+1); } this.id = (typeof this.id === 'undefined') ? getrandomid() : this.id; }
Comments
Post a Comment