0

If I have a directive for a table cell called

<table>
  <tr>
    <td cellDirective>Some cell Value</td>
    <td cellDirective>Another cell value</td>
    ...
  <tr> ...
<table>

defined by

myapp = angular.module('myapp', []);
myapp.directive('cellDirective', function() {
  return {
    link: function(scope, element) {
        console.log(element);
        element.addClass("coloring-class");
    }
  };
});

with the style

<style>
  .coloring-class {
    color: blue;
  }
</style>

What I get in the console is a reference to an object with a ton of different attributes, but I cannot find one with the value in each cell. So how can I access the value inside an element?

2 Answers 2

1

As per your JSBin, if you have the cell defined as

<td ng-repeat="cell in row" class="spreadsheet" cell="{{ cell }}">

you can define your directive as

clinApp.directive('cell', function() {
  return {
    restrict: 'AE',
    link: function(scope, element, attrs)  {
      console.log(attrs.cell);

attrs contains all the attributes in the current element where the directive is placed.

Sign up to request clarification or add additional context in comments.

1 Comment

That just returns undefined. You can check my changes to the bin, but I added console.log(attrs.cell) and the attrs parameter into my linking function.
0

It's just good old fashioned jQuery:

console.log(element.text());

3 Comments

how can I do this without jquery? I have an ng-repeat where the cell values are denoted {{ cell }}, and this function returns the text {{ cell }}. You can access this at jsbin.com/usecuf/13
So your example is a little different from the question. You can do one of two things - either use transclusion (docs.angularjs.org/guide/directive) to pass HTML into the directive, or define a template on the directive which contains {{cell}}.
Just to point out, the directive documentation on Angular is very dense but the examples are accurate. It took me a couple (very slow) reads before I understood building directives.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.