Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Few questions about directives
Message
De
27/02/2015 11:06:13
 
 
Information générale
Forum:
Javascript
Catégorie:
Autre
Divers
Thread ID:
01615790
Message ID:
01615913
Vues:
56
This message has been marked as a message which has helped to the initial question of the thread.
>>>>>>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.

pre-link is run on all directives then post-link for all directives. numberInputType is pre-link. Your directive is post-link (if, as you were doing, you don't specify either then it's post-link). So, regardless of priority the angular one would push the formatter first. If both are pre-link then the priority kicks in as expected.

>> 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.

It has to expect/populate a number in the model - that's the whole point of the 'type="number"'
The formatters job is to convert that number to a string for display in the control
The parsers job is to take the entered string from the control and convert it to a number in the model.

If your formatter runs after the angular formatter then it's guaranteed to receive a string. If, however, you are not using 'type="number" then it will receive a number. Your directive would also then need to implement a parser to convert the input to a valid number.

Does that make sense ?
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform