NgStyle返回:错误错误:找不到其他支持对象'{'background-color':'"blue"}' [英] NgStyle returns: ERROR Error: Cannot find a differ supporting object '{"background-color":"blue"}'

查看:247
本文介绍了NgStyle返回:错误错误:找不到其他支持对象'{'background-color':'"blue"}'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的模板中,

<span [ngStyle]="myStyle()"> HELLO </span>

这是我的组件中

myStyle(): string {
  return '{"background-color":"blue"}'
} 

我要

ERROR Error: Cannot find a differ supporting object '{"background-color":"blue"}'
    at KeyValueDiffers.webpackJsonp.../../../core/@angular/core.es5.js.KeyValueDiffers.find (core.es5.js:8051)
    at NgStyle.set [as ngStyle] (common.es5.js:2441)
    at updateProp (core.es5.js:11114)
    at checkAndUpdateDirectiveInline (core.es5.js:10806)
    at checkAndUpdateNodeInline (core.es5.js:12349)
    at checkAndUpdateNode (core.es5.js:12288)
    at debugCheckAndUpdateNode (core.es5.js:13149)
    at debugCheckDirectivesFn (core.es5.js:13090)
    at Object.View_AppComponent_2.currVal_2 [as updateDirectives] (AppComponent.html:10)
    at Object.debugUpdateDirectives [as updateDirectives] (core.es5.js:13075)
View_AppComponent_2 @ AppComponent.html:10
webpackJsonp.../../../core/@angular/core.es5.js.DebugContext_.logError @ core.es5.js:13415

推荐答案

原因是[ngStyle]不接受字符串.它接受一个对象.试试这个吧,

The reason is that [ngStyle] does not accept a string. It accepts an object. Try this instead,

myStyle(): object {
  return {"background-color":"blue"};
} 

这篇关于NgStyle返回:错误错误:找不到其他支持对象'{'background-color':'"blue"}'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆