1

I have some data being pulled in from an API in my controller with $http.get I then want to apply a text formatting filter to that text to make it all pretty.

However I am getting

TypeError: Cannot read property 'split' of undefined

all over my console.

Here's the code:

Controller $http get:

$http.get("url to my api data")
    .success(function (data, status, headers, config) {

        $scope.serviceStatus = data;

    })
    .error(function () {

    });

Filter within module:

app.filter('textFormat', function() {
    function format(input) {  
        // Call the passed in endpoint and at any capitalised letters, split it to have a space between words
        input = input.split(/(?=[A-Z])/).join(" ");

        // Get the character at index 0 and modify it to be uppercase. Then append the rest of the split string to the end
        input = input.charAt(0).toUpperCase() + input.slice(1);

        return input;
    };

    format.$stateful = true;

    return format;
});

HTML:

<div ng-repeat="services in serviceStatus">
    <p>The {{ services.service | textFormat }} is {{ services.status | textFormat }}</p>
</div>
3
  • I then want to apply a text formatting filter to that text to make it all pretty. This is not what filter should do. This is a directive's job Commented Dec 4, 2015 at 11:41
  • That's because your filter is called in every watch and before $http.get could receive the data, it has been called multiple times already. Commented Dec 4, 2015 at 11:47
  • 1
    @AnandG - There is no DOM manipulation here. Only simple text formatting. A filter is perfect fit to be used in such scenario. Commented Dec 4, 2015 at 11:49

2 Answers 2

3

You are getting this issue because of $http is an async call. It avoid from normal execution flow. You should change your filter code by below one

app.filter('textFormat', function() {
    function format(input) {  
     if(input) {  // Check if the value has been initialized
          // Call the passed in endpoint and at any capitalised letters, split it to have a space between words
          input = input.split(/(?=[A-Z])/).join(" ");

          // Get the character at index 0 and modify it to be uppercase. Then append the rest of the split string to the end
          input = input.charAt(0).toUpperCase() + input.slice(1);

          return input;
       }
    };

    format.$stateful = true; // You can put these two lines in above code as well. 

    return format;
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much @Vineet that's sorted two Filters I was having problems with in one go! Upvote to you sir!
0

You can not to use your filter function as a angular filter. The other solution is to place your filter function in your controller instead and then just call it from your view like this:

<div ng-repeat="services in serviceStatus">
<p>The {{ services.service | filter:textFormat }} is {{ services.status | filter:textFormat }}</p>
</div>

Be sure that textFormat is the name of your function

Comments

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.