In my code, I need to generate a random and numeric id and then assign to a parameter "id" in a different scope (i.escope.data = {id = "the random id"};
) but being new to Angular, I have no clue how to do this!
Asked
Active
Viewed 5,351 times
0

Vega
- 27,856
- 27
- 95
- 103

Dhruv Shrivastava
- 55
- 1
- 2
- 5
-
Well you can generate it just using plain javascript with no need of using AngularJS; have a look at this question for instance https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript – Dario Sep 10 '17 at 19:23
-
Yeah but I am pushing all the data over http (POST) to an api and a random id is a mandatory field so javascript should not be involved in this case, right? Because then If I generate a id using js, I will have to post the entire data using js and that would be pointless! – Dhruv Shrivastava Sep 10 '17 at 19:25
-
it's not really clear what you are tying to accomplish here. what "different scope" are you talking about, and why would you think posting data to a server wouldn't involve JavaScript? What do you mean by "If I generate a id using js, I will have to post the entire data using js"?? **Angular is JavaScript**.... – Claies Sep 10 '17 at 19:28
-
this is definitely a case where a [mcve] would go a long way toward conveying what you are trying to do. – Claies Sep 10 '17 at 19:31
-
Umm the codebase I am working on involves 100% Angular and I cannot make a post request with JS because of one field, right? There must be a way to generate an id in Angular right? – Dhruv Shrivastava Sep 10 '17 at 19:32
-
Your still aren't making sense. AngularJs is a **Framework** written in the JavaScript **Language**. ***EVERYTHING YOU DO IN ANGULAR IS JAVASCRIPT***. what does "I can't make a post request with JS because of one field" mean? How else would you make a post request?? – Claies Sep 10 '17 at 19:34
-
let's say, for example, you have an object `$scope.data`, and it has a bunch of properties, but needs an `id`. why wouldn't `$scope.data.id = randomNumberFunction();` work? – Claies Sep 10 '17 at 19:36
1 Answers
0
first you need to generate it, the best is to set your function in a service or in the $rootScope.
With this way you can call your service function or by the $rootScope.
First generate an numeric id (updated function refer to this post )
Create GUID / UUID in JavaScript?
I will show you the example with the 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 then service function in any controller scope, inject the service (guidservice)
and then call guidservice.getGuid();

Shiva kumar
- 673
- 8
- 23

Zakaria ASSANI
- 276
- 2
- 12
-
Can you please share an example for this? Would be really helpful, Thanks! – Dhruv Shrivastava Sep 10 '17 at 19:33