Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8857541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:36:53+00:00 2026-06-14T14:36:53+00:00

From a previous question I asked (http://stackoverflow.com/q/13383552/740318) I came full circle from creating a

  • 0

From a previous question I asked (http://stackoverflow.com/q/13383552/740318) I came full circle from creating a poorly designed filter, to a (still poorly developed, yet effective) directive, and in doing so, I realized it should have been done as a filter when properly implemented. Now the curious part is that (with my limited understanding) I’m going to have to re-establish a few of the “hackish” techniques implemented originally to get the filter to work when converting it.

The directive is as such:

widget.directive('truncate', function() {
  return {
    restrict: 'A',
    replace: true,
    // {{child.display}} will be a truncated copy of child.name
    template: '<a class="entity-name" href="{{child.url}}" title="{{child.name}}">{{child.display}}</a>',
    link: function(scope, element, attr) {
      var widthThreshold = $(element).parent().parent().width() * 0.85,
          // get the font-size without the 'px' at the end, what with media queries effecting font
          fontSize = $(element).css('font-size').substring(0, $(element).css('font-size').lastIndexOf('px')),
          sizeRatio = 29760/20621,
          characterCount = Math.floor((widthThreshold / fontSize) * sizeRatio);

      scope.$watch('child', function(val) {
        // Truncate it and trim any possible trailing white-space 
        var truncatedName = $.trim(scope.child.name.substring(0, characterCount));
        // Make sure characterCount isn't > the current length when accounting for the ...
        if (characterCount < scope.child.name.length + 3) {
          scope.child.display = truncatedName + '...';
        }
      });
    }
  }
});

For simplicity sake, I used jQuery to get the .width() of the element’s parent’s parent. This gives me the total screen width I have to work with. Depending on the aforementioned value (as well as things like the font-size CSS property), I truncate the string to display as much as reasonably possible, then end it with three periods (“…”). This was simple given that the element is passed in to the directive, and I could simply use $(element) with jQuery to get the identifier and work off that.

Now with the filter, I am restricted to only the string of characters being passed in, and no element as I did with a directive. With my limited knowledge, the only ways of accomplishing this in a filter are to either use the original (and likely process intensive) $('a:contains("' + name + '")') method implemented used before (in the previous question mentioned above), or maybe dynamically assign an id to each <a> and pass that in to the filter. Maybe something along the lines of (un-tested pseudo-code, hopefully it gets the point across):

<script>
function entityController($scope, $http) {
  $http.get('/path/to/json').success(function(res) {
    /* Sample return JSON ("uuid" is a unique database stored identifier):
     * {
     *   children: [
     *     { uuid:"123", url:"path/to/file", name:"File Name", display:"File Name" },
     *     { uuid:"234", url:"path/to/file2", name:"Second File Name", display:"Second File Name" },
     *     ...
     *   ]
     * }
     */
    $scope.children = res.children
  });
}
</script>
<body class="ng-controller:entityController" ng-controller="entityController">
  <div ng-repeat="child in children">
    <!-- Not sure if {{child.name | truncate:uuid}} would work, hopefully it conveys the point -->
    <a id="{{child.uuid}}" href="{{child.url}}" title="{{child.name | truncate:uuid}}">{{child.display}}</a>
  </div>
</body>

Now if I wanted to access it via jQuery, I would have to replicate the above directive’s functionality by doing something hackish such as:

var app = angular.module('app', []);

app.filter('truncate', function() {
  return function(str, uuid) {
    var widthThreshold = $('#' + uuid).parent().parent().width() * 0.85,
        fontSize = $('#' + uuid).css('font-size').substring(0, $('#' + uuid).lastIndexOf('px')),
        ...
});

It just seems to spiral downhill from there, where I’m continuing to query the DOM for more and more information that makes me contemplate whether or not a directive would be the improper (yet “cleaner”, if it were ever that) approach. When using a filter, is there a proper way to get access to DOM properties, such as the width of a parent element, or font sizes without having to abuse jQuery, and bulk up my syntax just to get access to the DOM in a filter?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:36:54+00:00Added an answer on June 14, 2026 at 2:36 pm

    AFAIK, there’s no way to pass $element into a filter via angular. Directives are what are meant for DOM manipulation. While it might feel like you’re just trying to shorten the string in your scope, you’re really trying to manipulate what’s being added to the DOM, so the directive is the right choice, IMO.

    That said, have you looked into the CSS text-overflow? It might do what you’re looking to do without JavaScript.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got http://jsfiddle.net/8p2Wx/2/ from a previous question I asked and I see these lines:
The previous question I asked about creating a DIV where size is calculated from
This is a continuation from the previous stackoverflow jQuery question I asked, but I
This stems from a previous question I asked - about a write conflict with
From a previous question on Stackoverflow Iterating through/Parsing JSON Object via JavaScript .... My
From the previous question I asked, I am still having the same question. I
From the previous question I asked, RemoveAll is the cleanest way to remove from
Following on from a previous question i asked, I'm now trying to figure out
This is the continual question from this previous question I've asked: Change Database Column
#&q=car&category=Car%20Audio%2CAccessories&brand= I borrowed a this function from a previous question asked on SO: function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.