0
I have a problem with my Money directive, it works like this: I make an Axios call to my API, the API brings me a value and I format to the BR standard.
But now I have to create a button atualizarthat is bringing me this formatting problem. Can anyone make it work please?
It is expected that when you click the button
getNewValue, the valuetestbe updated and formatted correctly
Jsfiddle: HERE
HTML
<div id="app">
<span v-money>{{test}}</span>
<button @click="getNewValue()">Get New Value</button>
</div>
JS
const formatNumber = (el) => {
let valorFinal = el.innerHTML;
if(valorFinal.includes(',')) valorFinal = valorFinal.replace(/\./g, "").replace(',','.');
el.innerHTML = parseFloat(valorFinal).toLocaleString('pt-BR', {minimumFractionDigits: 2,maximumFractionDigits: 2});
};
Vue.directive('money', {
inserted: function (el) {
let waitingAPI = setInterval(function () {
if (el.innerHTML != null && el.innerHTML != '') {
clearInterval(waitingAPI);
formatNumber(el);
}
}, 300);
},
update(el){
formatNumber(el);
}
})
const demo = new Vue({
el: '#app',
data:{
test: 10.25
},
methods:{
getNewValue(){
setTimeout( () => {
alert('Response Success API!')
this.test = 1300.25
},1000)
}
}
});
Thanks in advance :)
A doubt necessarily should be a span with the v-money directive? Or could it be an input? I’m not exactly sure I understand the problem, but I believe that altering would solve, I can also try to explain what I think is happening between the expected result and what is occurring.
– guastallaigor
It must necessarily be span with v-money directive, exactly as it is in the code. In this case it would be a Price Details screen. It’s not an input. From what I’ve seen is that after I give it innerHTML, it apparently loses its reactivity... This button
getNewValuewould be a simulation of a get in the API bringing a new bank value to that price. I don’t know how to resolve this ( formatting ) =/– Jackson
It would no longer be appropriate to use a filter?
– Tobias Mesquita