Sunday 15 January 2012

AngularJS custom filter for text/number -


I am trying to write my first custom filter for AngularJS. I want the ability to recognize that something is either a string or a number, if it is a number, then it has the built-in filter | Formats with the number .

I currently:

HTML

  & lt; Table & gt; & Lt; Tr ng-repeat = "data (key, val)" & gt; & Lt; TD & gt; {{}} Key & lt; / TD & gt; & Lt; Td ng-if = "isNumber (val)" & gt; {{Val | Number}} & lt; / TD & gt; & Lt; Td ng-if = "IsNumber (val)" & gt; {{Val}} & lt; / Td> & Lt; / TR & gt; & Lt; / Table & gt; Controller:  
  $ scope.data = {one: 5568 9, two: "Consider all options", three: 800243, four: "All over", five: "or just beginning"}; $ Scope.isNumber = function (value) {return angular.isNumber (value); };   

I thought this would be a better solution for assigning it because it has its own filter, though. This is what I have done so far (yes, I know that these are bare bones ... this is my first one).

  .filter ('textorumber', function () {return function (input) {If (input of typeof === 'number') {console.log ("I am a number" ); // formatted number as return form} else {console.log ("not a number"); // nothing; just return;} return input;}}}   

When it confirms that there is a number, can I apply the angular number to the filter? Or do I have to manually filter it with javascript?

I $ filter Service and then filter programmed numbers like this:

  angular.module ('filter', []). Filter ('textonumber', ['$ filter Return function ($ filter) return function (input, fractional) {if (hnnA (input)) {return input;} and {return $ filter ('number') (input, fractionSize);}}}}}} ]);   

In this way you will be able to use your textOrNumber filter in this way:

  {{myText | TextOrNumber: 4}}   

Working here

No comments:

Post a Comment