AngularJS 和 React比较

似乎已经有无数比较这两个流行的JavaScript框架的文章,和往常一样,你能发现来自两个派别的意见。目前看来,大多数情况下,一个开发者的观点,会在他们工作中大多数使用哪个库/框架/SDK等方面,往往是相当激进的。你使用它来工作,你习惯了使用它,你喜欢上了它,然后你就会在互联网上言辞激烈地争论有关于它的信息。

因此会出现一种像“AngularReact哪个更好”这样的问题,这类似于询问两个中的哪个恰好更流行,但这并不总是最佳衡量指标。所以需要选择的新手开发者该怎么办呢?真实情况是一个产品比另一个产品流行不能证明它就是更好的,我们可以看到许多这种例子的产品。

在这篇文章中,我不会询问哪个更好,而是通过例子比较这两个著名的框架,并让读者选择它们更喜欢的那个。因为在每一种各自的“代码风格”里面存在一个显著的差别。

Demo - Data Grid

我想创建一个简单的data grid而且希望可以增加、移除和更新在网格里面的数据,并且能够以JSON对象的方式来发送网格里面全部的数据(供服务器处理)

如下图

使用Angular实现 —— 分而治之

Angular JS基于MVC设计模式。我们先用Javascript创建一个控制器:

var app = angular.module("myApp", []);
app.controller("ResourceController", function($scope) {
$scope.resources = [{‘ResourceAddress‘: ‘http://www.discoversdk.com‘, ‘ResourceDescription‘:‘Great site‘}];
$scope.RemoveResource = function (index) {
$scope.resources.splice(index, 1);
}
$scope.AddResource = function (newres) {
if ($scope.resources == null) {
$scope.resources = {};
$scope.resources = [];
}
$scope.resources.push(
{
ResourceAddress: newres.ResourceAddress,
ResourceDescription: newres.ResourceDescription,
});
newres.ResourceAddress = "";
newres.ResourceDescription = "";
}
$scope.ShowRes = function () {
alert(JSON.stringify($scope.resources));
}
});
然后将标识的HTML文件与上面代码相关联:

<div ng-app="myApp" ng-controller="ResourceController">
<fieldset >
<h3>Add Resources</h3>
<span >Description:</span>
<input type="text" ng-model="newres.ResourceDescription" style="width:200px;" />
<span > URL:</span>
<input type="text" ng-model="newres.ResourceAddress" style="width:340px;" />
<a class="btn" href="#" ng-click="AddResource(newres)">Add</a><br/>

<h3>List of Resources</h3>
<table cellpadding="0" cellspacing="0">
<tbody>
<tr>
<th>Description</th>
<th>URL</th>
<th></th>
</tr>
<tr ng-repeat="res in resources">
<td><input style="width:200px;" type="text" ng-model="res.ResourceDescription" /></td>
<td><input style="width:440px;" type="text" ng-model="res.ResourceAddress" /></td>
<td><a class="btn" ng-click="RemoveResource($index)">Remove</a></td>
</tr>
</tbody>
</table>

<a class="btn" href="#" ng-click="ShowRes()">Show</a>
</fieldset>
</div>
然后用CSS文件添加样式:

body
{
font-family:Arial;
color:#3a3a3a;
}
table
{
border:none;
text-align:left;
margin-bottom:20px;
}
tr th
{
background-color: #3b97d3;
color: #fff;
padding: 5px;
border-right: solid 1px #3b97d3;
border-left: solid 1px #fff;
}
tr th:first-child
{
border-left: solid 1px #3b97d3;
}
tr td
{
padding:5px;
padding: 5px;
border-right: solid 1px #d4d4d4;
}
tr td:first-child
{
border-left: solid 1px #d4d4d4;
}
table tr:last-child td
{
border-bottom: solid 1px #d4d4d4;
}
input[type="text"]
{
height:20px;
font-size:14px;
}
a.btn
{
color:#fff;
background-color:#3b97d3;
padding:0px 10px;
height:26px;
display:inline-block;
line-height:26px;
text-decoration:none;
border-radius:3px;
-moz-border-radius:3px;
-webkit-border-radius:3px;
cursor:pointer;
}
a.btn:hover
{
background-color:#73c7ff;
}
你可以点击这里查看和运行代码

使用React实现——具有层次的GUI

我们通过React用层次结构组件构建用户界面,这主要的好处是代码复用

主要的区别是HTML,JavaScript,CSS代码混合到一起,但如果我们想在新的页面重用这个组件,我们只需要复制这一块的代码

所以在React里面,在我们的表格中我们这样在每一行构建一个List Item

var ResourceListItem = React.createClass({
getInitialState: function() {
return {ResourceDescription: this.props.children.ResourceDescription, ResourceAddress: this.props.children.ResourceAddress};
},
removeItem: function(e, index){
this.props.removeItem(resources.indexOf(this.props.children));
},
updateDescription : function(e){
resources[resources.indexOf(this.props.children)].ResourceDescription = e.target.value;
this.setState({ResourceDescription: e.target.value});
},
updateAddress : function(e){
resources[resources.indexOf(this.props.children)].ResourceAddress = e.target.value;
this.setState({ResourceAddress: e.target.value});
},
render: function(){
return (
<tr>
<td><input type="text" value={this.state.ResourceDescription} onChange={this.updateDescription} />{this.props.index}</td>
<td><input type="text" value={this.state.ResourceAddress} onChange={this.updateAddress} /></td>
<td><button onClick={this.removeItem.bind(null, this.props.children)}>remove</button></td>
</tr>
);
} });
接着我们构建表格组件:

var RecourceList = React.createClass({
render: function() {
var that = this;
var st = {
backgroundColor: ‘#3b97d3‘,
color: ‘#fff‘,
padding: ‘5px‘,
borderRight: ‘solid 1px #3b97d3‘,
borderLeft: ‘solid 1px #fff‘
};
var st2 = {
padding: ‘5px‘,
borderRight: ‘solid 1px #d4d4d4‘,
width: ‘250‘
};
var createItem = function(itemText, index) {
return (
<ResourceListItem removeItem={that.props.removeItem} key={index}>{itemText}</ResourceListItem>
);
};

return (
<table>
<thead>
<tr style={st}>
<td style={st2}>Description</td>
<td style={st2}>Url</td>
<td></td>
</tr>
</thead>
<tbody>
{this.props.items.map(createItem)}
</tbody>
</table>
)
} });
然后我们用同样的方式添加网格组件并声明我们的应用:

var MainApp = React.createClass({
getInitialState: function() {
return {items : resources};
},
addItems: function(newItem){
resources = resources.concat([newItem]);
this.setState({items: resources});
},
removeItem: function(itemIndex){
resources.splice(itemIndex, 1);
this.setState({items: resources});
},
show: function(){
alert(JSON.stringify(resources));
},
render: function(){
return (
<fieldset>
<div>
<h3>Add Resources</h3>
<AddResourceForm onFormSubmit={this.addItems} />
<a href="#" onClick={this.show}>Show</a>
<h3>List of Resources</h3>
<RecourceList items={this.state.items} removeItem={this.removeItem} />
</div>
</fieldset>
);
} });
你可以点击这里查看并运行代码(要注意的是jsfiddle的react支持有一点限制因此它会更改代码,你需要选择常规的JavaScript并选择回退JavaScript7来运行它)

原文:http://www.discoversdk.com/blog/angular-js-vs-react-by-example 作者:liran bh

时间: 2024-08-24 07:26:03

AngularJS 和 React比较的相关文章

angular4.0和angularJS、react.js、vue.js的简单比较

angularJS特性 模板功能强大丰富(数据绑定大大减少了代码量) 比较完善的前端MVC框架(只要学习这个框架,按照规定往里面填东西就可以完成前端几乎所有的的问题) 引入了Java的一些概念 angularJS的一些问题 性能问题[(脏检查机制)在angular中被废除了,整体性能被提升 路由问题[(使用的是第三方模块)在angular中路由器是主要的机制 作用域问题[(不能用原生的事件,想用就要调用一个方法)在angular中任何原生的事件都可以直接处理 表单验证问题[在angular中提供

vue.js、angularJS、react.js框架比较

下载链接:https://www.yinxiangit.com angularJS特性 模板功能强大丰富(数据绑定大大减少了代码量) 比较完善的前端MVC框架(只要学习这个框架,按照规定往里面填东西就可以完成前端几乎所有的的问题) 引入了Java的一些概念 angularJS的一些问题 性能问题 (脏检查机制)在angular中被废除了,整体性能被提升 路由问题 (使用的是第三方模块)在angular中路由器是主要的机制 作用域问题(不能用原生的事件,想用就要调用一个方法)在angular中任何

暂时放弃angularjs 学习React

视频学习地址: http://www.jtthink.com/course/play/575 官方地址 https://facebook.github.io/react/ cdn路径 <!DOCTYPE html> <html> <meta charset='utf-8'> <head> <title></title> <script src="http://cdn.bootcss.com/react/15.0.0/r

React复习小结(一)

一.React的发展 facebook在构建instagram网站的时候遇见两个问题: 1.数据绑定的时候,大量操作真实dom,性能成本太高 2.网站的数据流向太混乱,不好控制 于是facebook起初调研过市场上已存的mvc框架,发现都不太满意,于是就推陈出新,开发了react框架,并在2013年五月份开源. 二.React概述 和angularJS一样react核心解决的问题是数据绑定,开发者只要将数据绑定好,剩下的开发中只要关注业务就行了 1.组件化开发 使用react开发的时候,构建任何

初探React,将我们的View标签化

前言 我之前喜欢玩一款游戏:全民飞机大战,而且有点痴迷其中,如果你想站在游戏的第一阶梯,便需要不断的练技术练装备,但是腾讯的游戏一般而言是有点恶心的,他会不断的出新飞机.新装备.新宠物,所以,很多时候你一个飞机以及装备还没满级,新的装备就又出来了,并且一定是更强! 于是很多人便直接抛弃当前的飞机与装备,追求更好的,这个时候如果是人民币玩家或者骨灰级大神玩家的话,基本可以很快站在世界的顶端,一者是装备好,一者是技术好,但是我不愿意投入太多钱,也不愿意投入过多精力,于是在一套极品装备满级后会积累资源

说说React,Flux,Reray和GraghQL

前几天看到同事转发的<ReactEurope Conf 参会感想>,这篇文章讲的react的一些理念感觉有些道理,但我对react最终能很好的实现learn once, write everywhere还是持怀疑态度,毕竟世界是多样的,Apple的iOS(扁平化风格),Google的Android各自有自己的UI规范(Material Design和AngularJs Material Design ),即使React的理念很好,在别人的地盘上也不一定能弄出多大成果.但在Web前端领域,Rea

前端框架React Js入门教程【精】

现在最热门的前端框架有AngularJS.React.Bootstrap等.自从接触了ReactJS,ReactJs的虚拟DOM(Virtual DOM)和组件化的开发深深的吸引了我,下面来跟我一起领略ReactJS的风采吧~~ 章有点长,耐心读完,你会有很大收获哦~ 一.ReactJS简介 React 起源于 Facebook 的内部项目,因为该公司对市场上所有 JavaScript MVC 框架,都不满意,就决定自己写一套,用来架设 Instagram 的网站.做出来以后,发现这套东西很好用

vue和react总结

一.总结一下Vue和React的区别 相同点:   1.都支持服务器端渲染     2.都有Virtual DOM,组件化开发,通过props参数进行父子组件数据的传递,都实现webComponent规范     3.数据驱动视图     4.都有支持native的方案,React的React native,Vue的weex     5.都有管理状态,React有redux,Vue有自己的Vuex(自适应vue,量身定做) 不同点:   1.React严格上只针对MVC的view层,Vue则是M

一看就懂的ReactJs入门教程(精华版)

现在最热门的前端框架有AngularJS.React.Bootstrap等.自从接触了ReactJS,ReactJs的虚拟DOM(Virtual DOM)和组件化的开发深深的吸引了我,下面来跟我一起领略ReactJS的风采吧~~ 章有点长,耐心读完,你会有很大收获哦~ 一.ReactJS简介 React 起源于 Facebook 的内部项目,因为该公司对市场上所有 JavaScript MVC 框架,都不满意,就决定自己写一套,用来架设 Instagram 的网站.做出来以后,发现这套东西很好用