简体   繁体   中英

AngularJS - apply custom class directive to ng-repeat items

I'm trying to set the width of a div dynamically via AngularJS.

<ul id="contents">
  <li ng-repeat="content in contents">
    <div class="content_right custom_width">
      <div class="title">{{content.title}}</div>
    </div>
  </li>
</ul>

with the following directive

myApp.directive("custom_width", function() {
  return {
    restrict:"C",
    link: function(scope, element, attrs) {
      element.css("width", 400);
    }
  }
});

But nothing is happening. I try to "console.log" within the "link:function.." but nothing gets printed. What am I missing here?

Thanks for your time.

Directive definitions use camelCase:

myApp.directive("customWidth", function() {
  return {
    restrict:"C",
    link: function(scope, element, attrs) {
      element.css("width", 400);
    }
  }
});

Directive docs

In AngularJS, camel case ( lowerCamelCase ) is used when defining directives and accessing attributes (from within the directive), and snake case ( dash-case - whatever you want to call it) is used for referencing the directives and/or attributes in HTML elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM