Angular显示[ObjectObject]而不是值
创始人
2024-10-30 09:30:04
0

当在Angular中使用{{}}插值绑定时,如果绑定的值是JavaScript对象,并且没有定义toString()方法,则Angular默认会将其转换为[Object Object]字符串。为了解决这个问题,可以在对象的原型上定义toString()方法,以返回对象的所需字符串表示形式。例如:

在组件中定义对象:

foo = {
  name: 'John',
  age: 30
};

要在Angular模板中显示此对象的字符串表示形式,请将toString()方法定义为:

Object.prototype.toString = function() {
  var str = '';
  for (var prop in this) {
    if (this.hasOwnProperty(prop)) {
      str += prop + ': ' + this[prop] + ', ';
    }
  }
  return '{' + str.slice(0, -2) + '}';
};

这将返回这样的字符串:{name: John, age: 30}。现在,使用{{foo}}插值绑定时,Angular将显示正确的值。

相关内容

热门资讯

透视智能ai!aapoker怎... 透视智能ai!aapoker怎么开辅助器(透视)透视方法(本来是有挂);1、用户打开应用后不用登录就...
透视免费!aapoker透视脚... 透视免费!aapoker透视脚本入口(透视)ai插件(竟然是有挂)一、aapoker透视脚本入口AI...
透视数据!aapoker透视插... 透视数据!aapoker透视插件(透视)发牌逻辑(竟然真的有挂)1、下载好aapoker透视插件辅助...
透视透视挂!aapoker怎么... 透视透视挂!aapoker怎么设置抽水(透视)发牌逻辑(其实是真的有挂)aapoker怎么设置抽水辅...
透视教学!aapoker透视脚... 透视教学!aapoker透视脚本入口(透视)透视方法(其实存在有挂);在进入aapoker透视脚本入...
透视中牌率!aapoker透视... 透视中牌率!aapoker透视脚本(透视)透视脚本(确实真的是有挂)小薇(透视辅助)致您一封信;亲爱...
透视黑科技!aapoker透视... 透视黑科技!aapoker透视脚本(透视)ai插件(一贯存在有挂)1、aapoker透视脚本系统规律...
透视好友!aapoker透视脚... 透视好友!aapoker透视脚本入口(透视)辅助(其实真的是有挂)1、下载好aapoker透视脚本入...
透视讲解!aapoker透视脚... 透视讲解!aapoker透视脚本入口(透视)免费透视脚本(原来真的有挂)1)aapoker透视脚本入...
透视ai!aapoker辅助软... 透视ai!aapoker辅助软件合法吗(透视)万能辅助器(总是有挂)1、很好的工具软件,可以解锁游戏...