I have a problem in my Money directive, it works like this: I make an Axios call to my API, the API brings me a value and I format it to the default BR.
But now I had to create a atualizar
button that is bringing me this formatting problem. Does anyone know how to make this work please?
It is expected that when you click the
getNewValue
button, thetest
value is 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)
}
}
});
Thank you in advance:)