Angular 2 creating models vs working with json objects on the fly?

*爱你&永不变心* 提交于 2019-12-05 10:01:59

i suggest using models because :

  1. your code will be more readable for yourself after a while coming back to change it, every one else also can easily understand what you've done
  2. making changes in project will be more easily for example obj[0] does not have any special meaning but obj['username'] is more obvious
  3. you will get intellinsense in you IDE
  4. you can put logic in model for example so your controller will be more thin

    name: string
    age: number
    
    sayInfo(): string {
      return `name is ${this.name} and age is ${this.age}`
    }
    

    generally managing you app will be without headache (or at least less headache) :D

    just remember that fat models thin controllers

don't forget that passing more than five arguments to a function is not a good practice use an object instead for example :

constructor(file) {
  this.id = file['id']
  this.fileName = file['fileName']
  this.extention = file['extention']
  this.fileSize = file['fileSize']
  this.permission = file['permission']
  this.description = file['description']
  this.password = file['password']
  this.isFolder = file['isFolder']
  this.parent = file['parent']
  this.banStat = file['banStat']
  this.tinyLink = file['tinyLink']
  }
  getName(): string {
    return `${this.fileName}${(this.isFolder) ? '' : '.'}${this.extention}`
  }
  getIcon(): string {
    return this.isFolder ? 'fa-folder' : 'fa-music'
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!