【问题标题】:Ionic 3 set value in html templatehtml模板中的Ionic 3设置值
【发布时间】:2018-09-12 12:58:04
【问题描述】:

我正在尝试从我的 ts 文件中的 Ionic 3 模板中设置一个值,但是我同时也将 css 附加到它。我收到此错误

错误

错误:未捕获(承诺中):错误:表单控件没有值访问器,名称为:'image' 错误:没有名称的表单控件的值访问器:'image'

我的代码如下。

HTML

<img name="image" [(ngModel)]="image" id="image" src="https://openclipart.org/image/2400px/svg_to_png/247149/dual-compass-rose.png" alt=""> 

TS

this.image.style.transform = 'rotate(' + this.magneticHeading + 'deg)';

我不确定为什么这不起作用,但我开始认为 angular 不喜欢我在.style.transform... 的 TS 文件中尝试做的事情

【问题讨论】:

  • 是否有理由同时设置ngmodel和src?
  • 没有理由,但这会影响什么吗?

标签: javascript typescript ionic-framework angular-ngmodel


【解决方案1】:

使用@ViewChild 从你的 DOM 中引用图片标签。使用 [src] 而不是 ngModel:

打字稿:

  @ViewChild('myImage') myImage;
  magneticHeading = '100';
  image="https://openclipart.org/image/2400px/svg_to_png/247149/dual-compass-rose.png"
  ...
  ngAfterViewInit() {
    this.myImage.nativeElement.style.transform = 'rotate(' + this.magneticHeading + 'deg)';
  }

HTML:

  <img #myImage name="image" id="image" [src]="image" alt=""> 

DEMO

或者从 HTML 设置它:

  <img #myImage [style.transform]="'rotate(' + magneticHeading + 'deg)'" name="image" id="image" [src]="image" alt=""> 

DEMO

【讨论】:

  • 啊哈,这行得通,非常感谢@vega,标记为正确答案
  • 您的更新效果更好,图像旋转似乎更顺畅了。非常感谢您的时间 Vega,非常感谢
  • 我很高兴能够提供帮助:)
猜你喜欢
  • 2018-03-25
  • 1970-01-01
  • 2018-11-03
  • 1970-01-01
  • 1970-01-01
  • 2015-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多