我想在每次提交表单时生成一个格式为“ZXCVBN”的随机字符串

我想在每次提交表单时生成一个格式为“ZXCVBN”的随机字符串,并分配给 add-event.comonent.ts 文件中事件数组的“code”参数。我该怎么做?


代码必须正好包含 6 个大写随机字母。


添加-event.component.ts:-


export class AddEventComponent implements OnInit {


  event: Event = {

    code: '',

    name:'',

    password:'',

    pollCat:''

  }

  constructor(private eventService : EventsService) { }


  ngOnInit() {

  }


  onSubmit()

  {

    if(this.event.name !="" && this.event.password !="")

    {

      this.eventService.addEvent(this.event);

      this.event.name = '';

      this.event.password = '';

    }

  }

}

events.service.ts:-


  @Injectable({

  providedIn: 'root'

})

export class EventsService {


  eventsCollection : AngularFirestoreCollection<Event>;

  events: Observable<Event[]>;


  constructor(public afs: AngularFirestore) { 


    this.eventsCollection = this.afs.collection<Event>('Events');

    this.events = this.eventsCollection.snapshotChanges().pipe(

    map(changes => {

      return changes.map(a => {

        const data = a.payload.doc.data() as Event;

        data.id = a.payload.doc.id;

        return data;

      })

    })); 

  }


  getEvents()

  {

    return this.events;

  }


  addEvent(event: Event)

  {

    this.eventsCollection.add(event);

  }

}


繁花如伊
浏览 128回答 4
4回答

慕桂英546537

这是一个解决方案,将其添加到您的代码中function generateCode(length) {&nbsp; &nbsp; &nbsp; &nbsp;var result&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= '';&nbsp; &nbsp; &nbsp; &nbsp;var characters&nbsp; &nbsp; &nbsp; &nbsp;= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';&nbsp; &nbsp; &nbsp; &nbsp;var charactersLength = characters.length;&nbsp; &nbsp; &nbsp; &nbsp;for ( var i = 0; i < length; i++ ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result += characters.charAt(Math.floor(Math.random() * charactersLength));&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;return result;}console.log(generateCode(6))&nbsp; &nbsp;&nbsp;

MMTTMM

这应该是一个简单的方法:let randomString: string = ""const alphabetsString: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'&nbsp; &nbsp; for (let i = 0; i < 6; i++) {&nbsp; &nbsp; &nbsp; &nbsp; let randomNum: number =&nbsp; Math.floor(Math.random() * (25 - 0 + 1)) + 0;&nbsp; &nbsp; &nbsp; &nbsp; let newChar = alphabetsString.charAt(randomNum)&nbsp; &nbsp; &nbsp; &nbsp; randomString = randomString.concat(newChar)&nbsp; &nbsp; }//Return the value as per your program. I have used console.log(randomString)基本上我有一串字母,我使用了一个运行 6 次的循环。每次我得到一个介于 0 到 25 [都包括在内] 之间的随机数并使用charAt(). 所有的字母组合成一个字符串。

呼啦一阵风

[0,1,2,3,4,5,6].map(x=>'ABCDEFGHIJKLMNOPQRSTUVZ' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.substr(Math.floor(23*Math.random()),1)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.join('')

12345678_0001

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js">&nbsp; function generateRandomString(length) {&nbsp; &nbsp; var text = "";&nbsp; &nbsp; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";&nbsp; &nbsp; for (var i = 0; i < length; i++)&nbsp; &nbsp; &nbsp; text += possible.charAt(Math.floor(Math.random() * possible.length));&nbsp; &nbsp; return text;&nbsp; }</script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript