javascript - AngularJS - How to generate a random and unique numeric id and assign it to a parameter in a different scope? -
in code, need generate random , numeric id , assign parameter "id" in different scope (i.escope.data = {id = "the random id"};
) being new angular, have no clue how this!
first need generate it, best set function in service or in $rootscope.
with way can call service function or $rootscope.
first generate numeric id (updated function refer post )
create guid / uuid in javascript?
i show example service :
(function () { 'use strict';
angular .module('your module app name') .factory('guidservice', guidservice); guidservice.$inject = ['$rootscope']; function partenairesservice($rootscope) { var service = { getguid: getguid, }; return service; /* * return numeric unik id of 16 chr. */ function getguid() { function s4() { return math.floor((1 + math.random()) * 0x10000); } return s4() + ''+ s4() + '' + s4() + '' + s4() + '' + s4() ; } } })();
to use service function in controller scope, inject service (guidservice)
and call guidservice.getguid();
Comments
Post a Comment