0

I have an angular expression in my html which returns some data:

      <span ng-SOMTHING="stripFirstLetters">
            {{thing.data}}
      </span>

At the minute, the data being returned has 5 letters at the beginning which I want to remove. Can I accomplish this with ng-pattern?

So in my controller, somthing like this:

      $scope.stripFirstLeters = /^\d{5}/; 

I just want to know if what I'm trying to do is possible... Thanks!

2
  • ng-pattern is for input elements. Commented Mar 12, 2014 at 14:45
  • 1
    the regex would be /^\d{5}/. Commented Mar 12, 2014 at 14:45

1 Answer 1

3

No.

ng-pattern isn't a directive. Rather it is an argument of the input directive (ref). Using it standalone would have no effect.

You can always accomplish the same result using a filter, with sample usage:

<span>{{ thing.data | strip5 }}</span>

The filter implementation itself would be trivial:

app.filter("strip5", function() {
    return function(x) {
        if( typeof(x) !== "string" || x.length < 5 ) return "";
        return x.substring(5);
    };
});
Sign up to request clarification or add additional context in comments.

3 Comments

Trivial for you perhaps :) But thanks all the same, works a treat!
If I wanted to strip away all characters up to and including the first space, how would that filter operate?
Just replace the content of the filter function with code that does what you want, e.g. var index = x.indexOf(" ");return x.substring(x+1); with some error checking.

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.