Dirty Checking Angularjs and Vuejs

Asked

Viewed 64 times

3

I really like the Angularjs, and when I read in articles about its discontinuation, it was that one of the main reasons was Dirty Checking. I think that Dirty Checking are the checks made by Two way data Binding, wanted to know if the Vuejs owns this Dirty Checking, because I lay seeing on the outside, I do not see the difference between the two codes. For me the two are two way data Binding

Using Vuejs

var vm = new Vue({
  el: '#app',
  data: {
  teste: "Teste"
  }
});
<script src="https://cdn.jsdelivr.net/vue/1.0.16/vue.js"></script>

<div id="app">
  <input v-model="teste">
  {{teste}}
</div>

Using Angularjs

var app = angular.module('app', []);

app.controller('MainCtrl', function($scope) {
  $scope.teste = 'Teste';
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app" ng-controller="MainCtrl">
  <input ng-model="teste">
  <p>{{ teste }}</p>
</div>

  • 2

    Good question! After the publications I saw here today on the site, I wish I could raise this one 20 times.

  • I’m sorry, I don’t know Angularjs, but where did you read about him being discontinued? I mean, I think you read from somewhere that might have some wrong information. Not that they do not promote Angular (without JS), but I believe that Angularjs still gets improvements, but it would be interesting if you put yourself from where you got this information.

  • I believe that AngularJS is version 1. Last week came out Angular 5.

  • AngularJS is a thing. Angular is another. The AngularJS is called Angular 1 because it was the first version, but not to confuse, the people of Google separated the AngularJS and Angular. The Angular (Already in version 5), is a rewritten framework from scratch, it has totally different concepts from AngularJS

1 answer

1


From what I know of Angular o ng-model and the v-model are the same idea. A quick search confirmed this.

There is much talk today of unidirectional data Binding, which means that the state/data mutations must occur in one direction, in a flow from A to B. In particular: from the holder of that state (the Store, the data or computed in Vue/Vuex).

This idea/rule is useful, but there are exceptions worth considering, such as the eval can be useful somewhere. This is one of them.

The v-model allows the this.teste of the component is changed when the input change, and allows the input be changed when the this.teste change. This can generate bugs, because Vue reacts to these changes, and be difficult to "debug".

Still, the v-model is not mandatory. It is perfectly possible to make a closed cycle of state to avoid v-model. A minimalist example would be:

new Vue({
  el: '#app',
  data: {
    value: ''
  },
  watch: {
    value(val) {
      console.log('Novo value!', val);
    }
  },
  methods: {
    updateValue(e) {
      e.preventDefault();
      this.value = e.target.value;
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.3/vue.min.js"></script>
<div id="app">
  <input type="text" :value="value" @input="updateValue" placeholder="Escreve aqui...">
  <p>{{value}}</p>
</div>

However, I think it is practical, and limited to specific cases, it is useful.

The same idea with v-modal:

new Vue({
  el: '#app',
  data: {
    value: ''
  },
  watch: {
    value(val) {
      console.log('Novo value!', val);
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.3/vue.min.js"></script>
<div id="app">
  <input type="text" v-model="value" placeholder="Escreve aqui...">
  <p>{{value}}</p>
</div>

  • I liked the answer! But you still haven’t made me very clear... As you said, v-model and ng-model are very similar. And indeed, they are! But because in so many places it says that ng-model made a bad Dirt Cheking, and the v-model No? What the AngularJS had it so bad in yours two way data Binding compared to VueJS?

  • 1

    @Jackson does not know the internal code of the Angular but from what I read he does Dirty checking when it fetches the value of the elements that use this ng-model. And reading this comment from Vue’s creators who worked at Google and knows Angular well the Vue does not use Dirty checking.

  • Interesting! From what I read the Vue does not use the Dirty Cheking, and yes Observable pattern. I read this question that I found very interesting! Worth reading: https://stackoverflow.com/questions/24698620/dirty-checking-on-angular

Browser other questions tagged

You are not signed in. Login or sign up in order to post.