10

I am using a data table created mostly with css that I found online. In one of the columns there is a css data attribute "data-title" which is assigned a string.

<td data-title="someString">

When I enter a string, the styling inside the column works as expected. When I try to bind to an objects string, the binding doesn't work like I would expect. I tried

<td data-title="object.someString">

which just displays literal 'object.someString' and I tried

<td data-title="{{object.someString}}">

which displays nothing (blank). Any idea why my binding isn't working here?

CSS:

.responsive-table tbody td[data-title]:before {
  content: attr(data-title);
  float: left;
  font-size: .9em;
  color: #565248;
}

@media (min-width: 48em) {
  .responsive-table tbody td[data-title]:before {
    content: none;
  }
}
4
  • Please post the relevant CSS. Is it using the td::before { content: attr('data-title'); } property and function? Commented Sep 6, 2017 at 0:01
  • @Dai edited with relevant CSS Commented Sep 6, 2017 at 0:02
  • Please undo your edit that incorrectly refers to the attributes as "CSS selector attributes". The data- attributes are not "CSS selector attributes". CSS selectors can use any element attribute. Commented Sep 6, 2017 at 0:06
  • Got it. Changed to data attribute Commented Sep 6, 2017 at 0:08

1 Answer 1

26

Angular 2 doesn't bind to data- attributes using the simpler {{ }} syntax because it tries to map them to DOM properties, and there isn't a DOM property for data-title (and it seems Angular2 isn't smart enough to use dataset yet - unless I'm missing something).

So to bind to data- attributes you need to use the attr.data-title={{}} or [attr.data-...]="" syntax. See this QA: Angular 2 data attributes

<td [attr.data-title]="object.someString">

Or:

<td attr.data-title="{{object.someString}}">
Sign up to request clarification or add additional context in comments.

1 Comment

Can i also bind a function to data-* attribute? I've tried but it just doesn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.