Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Few questions about directives
Message
 
 
À
27/02/2015 05:57:46
Information générale
Forum:
Javascript
Catégorie:
Autre
Divers
Thread ID:
01615790
Message ID:
01615909
Vues:
49
>>>>>I thought I'd posted something after this but anyway:
>>>>>
>>>>>I tested using both positive and negative priority and your directive always ran first. I *did* get it to work by changing your formatter to:
>>>>>                    function bind() {
>>>>>                        return ngModel.$formatters.unshift(function (value) {
>>>>>                            return format(value);
>>>>>                        });
>>>>>                    }
Don't know why the priority approach didn't work but this shows that your formatter needs to be first in the $formatters array.....
>>>>
>>>>Wow, thanks for the clever solution, Viv. Seems like that error is gone although I'll do more tests.
>>>
>>>It's OK in this case since you always want your formatter to run after the angular one - but if you ever want to add another formatter after it then you are back to square one - i.e. needing to control the order of the relevant directives.
>>>
>>>Wish I knew why setting priority doesn't seem to work :-{
>>
>>Could it be a bug?
>
>Think I worked it out:
>
>The link function needs to be run pre-link with a priority > 0. i.e:
app.directive('smNumberFormat', ['$filter', function ($filter) {
>            return {
>                restrict: 'A', // only activate on element attribute
>                require: '?ngModel', // get a hold of NgModelController
>                priority:  100,
>                link: {
>                    pre: function (scope, element, attrs, ngModel) {
>                    if (!ngModel) return; // do nothing if no ng-model
>
>                    //Set the default decimal place to zero, if accuracy is not specified
>                    var accuracy = parseInt(attrs.accuracy) || 0;
>                    var stripCommas = attrs.stripCommas || true;
>
>                    ngModel.$render = function () {
>                        return element.val(ngModel.$viewValue);
>                    };
>
>                    var format = function (newValue) {
>                        console.log("Running snNumberFormat formatter");
>                        if (accuracy > 0) {
>                            newValue = parseFloat(newValue);
>                        } else {
>                            newValue = parseInt(newValue);
>                        }
>
>                        //Let's not show any value if it's zero or not a number, so the placeholder text is displayed instead
>                        if (isNaN(newValue)) return undefined;
>
>                        newValue = $filter('number')(newValue, accuracy);
>
>                        if (stripCommas) {
>                            newValue = newValue.split(",").join("");
>                        }
>                        return newValue;
>                    };
>
>                      function bind() {
>                          return ngModel.$formatters.push(function (value) {
>                            return format(value);
>                        });
>                    }
>
>                    bind();
>
>                    element.on('blur', function () {
>                        return element.val(format(ngModel.$viewValue));
>                    });
>                }}
>

Thanks a lot, Viv. I haven't yet come across pre link, will need to read about it. I'll implement your new change.

> BTW, would you agree that for numberInput we probably don't need to check for actual number, we may just check for the value (could be a string) being a number. If that code would have checked that instead of isNumber function, then I would not have a problem.
>
>Not sure what you mean. If you're using 'type="number"' then angular ensures that the value received by your formatter will be the string value of a valid number.
>If you're not using 'type="number"' then (assuming the model value is a number) then you will receive a number.

The angularJs checks for the value to be the actual number, not the string value which is number. If the inputNumber would check the string value as number, then "0.00" would be valid and this thread would not exist.
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform