Angular2向元素添加属性

时间:2016-11-25 13:41:56

标签: angular

如何在创建元素时为元素添加属性?

我尝试了以下Dashboard组件:

import { Component, ElementRef, Renderer  } from '@angular/core';

@Component({
  selector: '[re-dashboard]',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.scss']
})
export class DashboardComponent {

  constructor (private _el: ElementRef, private _renderer: Renderer) {
    _renderer.setElementAttribute(_el, 're-component','');
  }
}

我想在创建DashboardComponent时将re-component属性添加到div。然后我使用re-component来设置创建的div的样式。但是我收到以下错误:http://pastebin.com/up35zmUv

任何想法

祝你好运

1 个答案:

答案 0 :(得分:2)

试试这个:

var path3 = arcGroup.selectAll(".arc"),
    startPoint = pathStartPoint(path3)

var marker = arcGroup.append("circle")
marker.attr("r", 7)
  .attr("transform", "translate(" + startPoint + ")")

transition();

function pathStartPoint(path) {
  var d = path.attr("d")
  console.log(path)
  dsplitted = d.split(" ");
  return dsplitted[0].split(",");
}

function transition() { 
  marker.transition()
    .duration(7500)
    .attrTween("transform", translateAlong(path3.node()))
    .each("end", transition);// infinite loop
}

function translateAlong(path) {
  var l = path.getTotalLength();
  return function(i) {
    return function(t) {
      var p = path.getPointAtLength(t * l);
      return "translate(" + p.x + "," + p.y + ")";//Move marker
    }
  }
}