【问题标题】:Bind Angular2 values in SVG linear gradient stop offset?在 SVG 线性渐变停止偏移中绑定 Angular2 值?
【发布时间】:2017-04-25 17:32:33
【问题描述】:

我想在线性梯度停止偏移中使用绑定 angular2 值,但它给了我错误。有人可以告诉我如何在线性渐变的停止偏移中绑定 angular2 值,如下所示?

test.component.ts

import {Component, EventEmitter, ViewEncapsulation, Input, OnInit} from '@angular/core';


@Component({
selector: 'test-comp',
templateUrl: 'test-component.html',
encapsulation: ViewEncapsulation.None
})

export class TestComponent implements OnInit {

@Input() name: string;
@Input() flexScore: number;

protected goodScore: number;
protected dangerScore: number;
protected isComplete: boolean = false;

 ngOnInit() {
  this.goodScore = this.flexScore;
  this.dangerScore = 100 - this.goodScore;
  console.log(this.goodScore + ' ' + this.dangerScore);
  this.isComplete = true;
 }
}

测试组件.html

<div class="individual-athlete-risk">
  <span id="name">{{name}}</span>
  <span id="score">{{flexScore}}</span>
</div>
<svg width="200" height="10" xmlns="http://www.w3.org/2000/svg">
  <defs>
      <linearGradient id="testGradient">
          <stop attr.offset="{{goodScore}}%" stop-color="blue"/>
          <stop attr.offset="{{dangerScore}}%" stop-color="red"/>
      </linearGradient>
  </defs>
  <rect fill="url(/test#testGradient)" x="0" y="0" width="200" height="9"/>
</svg>

它给了我错误。我想添加具有动态值的渐变线。请帮忙。

@Gaunter 我已经更新/编辑了您所说的代码,现在已删除错误,但根据 OnInit() 函数中确定的值,条形/渐变似乎仍然是恒定的。我检查了 OnInit 中的 goodScore 和 dangerScore 值,它们是正确的并且不统一。有什么想法吗?

【问题讨论】:

    标签: javascript angular svg


    【解决方案1】:

    我想这就是你想要的:

          <stop [attr.offset]="goodScore" stop-color="blue"/>
          <stop [attr.offset]="dangerScore" stop-color="red"/>
    

    您需要[attrname]="fieldName"attrname="{{fieldName}}" 才能获得Angular2 绑定。
    SVG 元素没有属性,因此需要进行属性绑定,因此需要额外的 attr. 前缀来绑定到 SVG 元素。

    【讨论】:

    • 非常感谢。没错,这就是我想要的
    • 我已经更新了上面的代码,删除了错误,但梯度线/条对于每个值仍然是恒定的。有什么疯狂的猜测吗?
    • 你能创建一个 Plunker 吗?这使得调查更容易。 Plunker 有一个 Angular2 TS 模板。
    • 请找到样品/测试插件。 plnkr.co/edit/tpl:AvJOMERrnz94ekVua0u5?p=preview
    • 那行不通。好像没有保存。这只是模板代码。
    猜你喜欢
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    • 2020-11-20
    相关资源
    最近更新 更多