简体   繁体   中英

How would I go about adding a custom filter in a directive?

So I'm pretty new to Angular Js. I'm trying to accomplish a simple problem. I just need to truncate a string of text. I know there is the limitTo filter and I could just attach it to an element, but this needs to be dynamic.

这是我的HAML文件

这是我的角度文件

Here is also a link to the pen codepen.io/Brushel/pen/QMXPWN?editors=1010

Here is a simple example implementing all the requirements, use this template to build your directive.

app.directive('truncate', function() {
  function link(scope, element, attrs){
    console.log(scope.input)
    console.log(scope.maxCharacters)
  }

  return{
    restrict: 'A',
      scope: {
      input: '=',
      maxCharacters: '=',
      href: '=',
      isShowMore: '='
      },
    template: '<h1 ng-init="limit=true;length=maxCharacters">{{input | limitTo: length}}<a ng-attr-href="{{ href ? \'#\': undefined }}" ng-click="limit=!limit;length=limit?maxCharacters: \'\'">{{isShowMore?"Show More":"..."}}</a></h1>',
    link: link
  }

});

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