search
HomeWeb Front-endFront-end Q&AWhat is the difference between angularjs and vuejs

Difference: 1. Angularjs uses "dirty value detection" to compare whether the data has changed and realizes two-way data binding; while vue uses "data hijacking" combined with "publisher-subscriber mode" way to achieve two-way data binding. 2. The vue instruction uses the "v-" operator, and the angularjs instruction uses "ng-".

What is the difference between angularjs and vuejs

The operating environment of this tutorial: windows7 system, vue2.9.6&&Angular9 version, DELL G3 computer.

Comparison between using Angularjs and Vue.js

The previous projects all used Angularjs, (please note that this article mainly talks about Angularjs 1) After the initial use of Vue.js A simple comparison note.
First of all, let’s briefly talk about their respective characteristics in theory, and then use a few small examples to illustrate them.

Angular

  • 1, MVVM (Model) (View) (View-model)
  • 2, Module control Controller (Contoller) dependency injection:
  • 3, two-way data binding: interface operations can be reflected in the data in real time, and data changes can be displayed in the interface in real time.
  • 4, command (ng-click ng-bind ng-model ng-href ng-src ng-if/ng-show...)
  • 5, service Service($compile $ filter $interval $timeout $http...)
  • 6, routing (ng-Route native routing), ui-router (routing component)
  • 7, Ajax encapsulation ($http)

The implementation of two-way data binding uses dirty value detection of $scope variables, using $scope.$watch (view to model), $scope.$apply (model to view) detection, internal All calls are digest. Of course, you can also call $scope.$digest directly for dirty checking. It is worth noting that when data changes very frequently, dirty detection will consume a lot of browser performance. The official maximum dirty detection value is 2000 pieces of data.

Vue

vue.js official website: It is a progressive framework for building user interfaces. Unlike other heavyweight frameworks, Vue adopts a bottom-up incremental development design. Vue's core library only focuses on the view layer, and is very easy to learn and integrate with other libraries or existing projects. Vue, on the other hand, is fully capable of driving complex single-page applications developed using single-file components and Vue ecosystem-supported libraries. The goal of Vue.js is to implement

responsive data binding

and composed view components through the simplest possible API.

(1) Modularization. Currently, the hottest way is to use ES6 modularization directly in the project and combine it with Webpack for project packaging.
  • (2) Componentization to create a single Files with component suffix .vue, including template (html code), script (es6 code), style (css style)
  • (3) Two-way data binding: The operation of the interface can be reflected in the data in real time. Changes can be displayed on the interface in real time.
  • (4) Command (v-html v-bind v-model v-if/v-show...)
  • (5) Routing (vue-router)
  • (6) vuex data sharing
  • (7) Ajax plug-in (vue-resource, axios)
  • vue is very small, the min source code after compression is 72.9kb, after gzip compression It is only 25.11kb, which is 144kb compared to Angular. You can use it by yourself with the required library plug-ins, such as routing plug-ins (Vue-router), Ajax plug-ins (vue-resource, axios), etc.

Principle of two-way data binding between Vue and Angular

angular.js: Dirty value check

angular. js uses dirty value detection to compare whether the data has changed to decide whether to update the view. The simplest way is to regularly poll to detect data changes through setInterval(). Of course, Google will not be so slow. Angular can only detect data changes at specified events. Enter dirty value detection when triggered, roughly as follows:

    DOM events, such as the user inputting text, clicking a button, etc. (ng-click)
  • XHR response event ($http)
  • Browser Location change event ($location)
  • Timer event ($timeout, $interval)
  • Execute $digest() or $apply()

vue: Data hijacking

vue.js is Using data hijacking combined with the publisher-subscriber model, Object.defineProperty() is used to hijack the setters and getters of each property, publish messages to subscribers when the data changes, and trigger the corresponding listening callbacks. https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/definePropertydefineProperty

The code is directly below

The first is of course Hello World

vue

<div id="app">
  {{ message }}
</div>

new Vue({
  el: &#39;#app&#39;,
  data: {
    message: &#39;Hello Vue.js!&#39;
  }
})

Angular

<div ng-app="myApp" ng-controller="myCtrl">
 {{message}}
</div>

var app = angular.module(&#39;myApp&#39;, []);
app.controller(&#39;myCtrl&#39;, function($scope) {
    $scope.message = "Hello world";
});

In comparison, vue uses the json data format to write dom and data, and the writing style is more based on the js data encoding format. , easy to understand.

Vue’s two-way data binding

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

new Vue({
  el: &#39;#app&#39;,
  data: {
    message: &#39;Hello Vue.js!&#39;
  }
})

Angular’s ​​two-way data binding

<div ng-app="myApp" ng-controller="myCtrl">
  <p>{{message}}</p>
  <input ng-model="message">
</div>

var app = angular.module(&#39;myApp&#39;, []);
app.controller(&#39;myCtrl&#39;, function($scope) {
    $scope.message = "Hello world!";
});

vue is a lightweight Level framework does provide a lot of APIs, including some convenient instructions and attribute operations. Generally, Vue instructions use the (v-) operator, compared to angularjs instructions that use (ng-). Among them, vue.js also supports the abbreviation of instructions:

  • (1) event click

    <a v-on: click="fn"></a>

    简写方式:
    <a></a>

  • (2)属性

    <a v-bind: href="url"></a>

    简写方式:
    <a :href="url"></a>

vue.渲染列表

<div id="app">
  <ul>
    <li v-for="name in names">
      {{ name.first }}
    </li>
  </ul>
</div>

new Vue({
  el: &#39;#app&#39;,
  data: {
    names: [
      { first: &#39;summer&#39;, last: &#39;7310&#39; },
      { first: &#39;David&#39;, last:&#39;666&#39; },
      { first: &#39;Json&#39;, last:&#39;888&#39; }
    ]
  }
})

Angularjs渲染列表

<div ng-app="myApp" ng-controller="myCtrl">
  <li ng-repeat="name in names">{{name.first}}</li>
</div>

var app = angular.module(&#39;myApp&#39;, []);
app.controller(&#39;myCtrl&#39;, function($scope) {
    $scope.names = [
      { first: &#39;summer&#39;, last: &#39;7310&#39; },
      { first: &#39;David&#39;, last:&#39;666&#39; },
      { first: &#39;Json&#39;, last:&#39;888&#39; }
    ]
});

vue的循环

<ul>
    <li v-for="item in list">
        <a :href="item.url">{{item.title}}</a>
    </li>
</ul>

angular和vue的渲染差不多

<div class="item" ng-repeat="news in  newsList">
    <a ng-href="#/content/{{news.id}}">
        <img  src="/static/imghwm/default1.png"  data-src="{{news.img}}"  class="lazy"  ng- / alt="What is the difference between angularjs and vuejs" >
        <div class="item-info">
            <h3 id="news-title">{{news.title}}</h3>
            <p class="item-time">{{news.createTime}}</p>
        </div>
    </a>
</div>

vue和Angular处理用户输入

<div id="app">
  <p>{{ message }}</p>
  <button v-on:click="reverseMessage">Reverse Message</button>
</div>

new Vue({
  el: &#39;#app&#39;,
  data: {
	message: &#39;Hello Vue.js!&#39;
  },
  methods: {
    reverseMessage: function () {
      this.message = this.message.split(&#39;&#39;).reverse().join(&#39;&#39;)
    }
  }
})
<div ng-app="myApp" ng-controller="myCtrl">
 <p>{{ message }}</p>
 <button ng-click="reverseMessage()">Reverse Message</button>
</div>

var app = angular.module(&#39;myApp&#39;, []);
app.controller(&#39;myCtrl&#39;, function($scope) {
    $scope.message = "Hello world!";
    $scope.reverseMessage = function() {
        this.message = this.message.split(&#39;&#39;).reverse().join(&#39;&#39;)
    }
});

相关推荐:《vue.js教程

The above is the detailed content of What is the difference between angularjs and vuejs. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
VUE3快速入门:使用Vue.js指令实现选项卡切换VUE3快速入门:使用Vue.js指令实现选项卡切换Jun 15, 2023 pm 11:45 PM

本文旨在帮助初学者快速入手Vue.js3,实现简单的选项卡切换效果。Vue.js是一个流行的JavaScript框架,可用于构建可重用的组件、轻松管理应用程序的状态和处理用户界面的交互操作。Vue.js3是该框架的最新版本,相较于之前的版本变动较大,但基本原理并未改变。在本文中,我们将使用Vue.js指令实现选项卡切换效果,目的是让读者熟悉Vue.js的

VUE3基础教程:使用Vue.js插件封装图片上传组件VUE3基础教程:使用Vue.js插件封装图片上传组件Jun 15, 2023 pm 11:07 PM

VUE3基础教程:使用Vue.js插件封装图片上传组件Vue.js是一款流行的前端框架,它使开发者可以用更少的代码创建更高效、灵活的应用程序。尤其是在Vue.js3发布之后,它的优化和改进使得更多的开发者倾向于使用它。这篇文章将介绍如何使用Vue.js3来封装一个图片上传组件插件。在开始之前,需要先确保已经安装了Vue.js和VueCLI。如果尚未安装

VUE3基础教程:使用Vue.js插件封装日历组件VUE3基础教程:使用Vue.js插件封装日历组件Jun 15, 2023 pm 09:09 PM

Vue.js是现代化的前端JavaScript框架之一,它提供了一套完整的工具来构建交互式用户界面。在Vue.js的生态系统中,有各种各样的插件和组件,可以大大简化我们的开发流程。在本篇文章中,我们将介绍如何使用Vue.js插件封装一个日历组件,以方便我们在Vue.js项目中快速使用。Vue.js插件Vue.js插件可以扩展Vue.js的功能。它们可以添加全

Vue.js实现登录验证的完整指南(API、JWT、axios)Vue.js实现登录验证的完整指南(API、JWT、axios)Jun 09, 2023 pm 04:04 PM

Vue.js是一种流行的JavaScript框架,用于构建动态Web应用程序。实现用户登录验证是开发Web应用程序的必要部分之一。本文将介绍使用Vue.js、API、JWT和axios实现登录验证的完整指南。创建Vue.js应用程序首先,我们需要创建一个新的Vue.js应用程序。我们可以使用VueCLI或手动创建一个Vue.js应用程序。安装axiosax

VUE3开发入门教程:使用Vue.js组件封装chart图表VUE3开发入门教程:使用Vue.js组件封装chart图表Jun 15, 2023 pm 10:29 PM

随着大数据时代的到来,数据可视化已经成为了现如今的趋势之一。在Web前端开发的过程中,如何使用Vue.js进行数据可视化处理,成为了许多前端开发者所关注的问题。本文将会介绍如何使用Vue.js组件,封装基于chart.js库的图表。1.了解chart.jsChart.js是一款基于HTML5CanvasElement的简单易用、跨平台的开源图表库,我们可

VUE3基础教程:使用Vue.js自定义事件VUE3基础教程:使用Vue.js自定义事件Jun 15, 2023 pm 09:43 PM

Vue.js是一款流行的JavaScript框架,它提供了很多方便的特性,所以它在开发Web应用程序时非常有用。Vue.js中的自定义事件系统使其更加灵活,并且可以通过组件事件触发和处理来实现更好的代码重用性。在本文中,我们将讨论如何使用Vue.js的自定义事件。Vue.js中自定义事件的基础在Vue.js中,我们可以通过v-on指令来监听DOM事件。例如,

VUE3开发入门:使用Vue.js动态过滤数据列表VUE3开发入门:使用Vue.js动态过滤数据列表Jun 15, 2023 pm 09:10 PM

Vue.js已经成为现代Web开发的中流砥柱。它是一个轻量级的JavaScript框架,提供了数据绑定和组件化的能力,使得开发者能够更加轻松地构建交互型应用程序。而现在,Vue.js的新版本VUE3也已经面世。在本文中,我们将使用VUE3,通过实例,介绍如何在Vue.js中实现动态过滤数据列表。1.准备工作在开始本教程之前,您需要先安装Node.js和Vue

2022年最新5款的angularjs教程从入门到精通2022年最新5款的angularjs教程从入门到精通Jun 15, 2017 pm 05:50 PM

Javascript 是一个非常有个性的语言. 无论是从代码的组织, 还是代码的编程范式, 还是面向对象理论都独具一格. 而很早就在争论的Javascript 是不是面向对象语言这个问题, 显然已有答案. 但是, 即使 Javascript 叱咤风云二十年, 如果想要看懂 jQuery, Angularjs, 甚至是 React 等流行框架, 观看《黑马云课堂JavaScript 高级框架设计视频教程》就对了。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)