将视图(页面)分解成布局和模板视图,再根据当前URL展示对应视图

布局模板

1
2
3
< header>< h1>Header</h1></header>
< div class="content" >
< div ng-view>< /div>< /div >

ng-view 由ngRoute模块提供的一个特殊指令, 在HTML中给$route对应的视图内容占位。优先级1000

  • 每次触发$routeChangeSuccess事件, 视图会更新
  • 如果某个模板同当前的路由关联
    • 创建新作用域
    • 移除上一视图和作用域
    • 将新作用域与当前模板关联
    • 如果路由中控制器定义,则与当前作用域关联
    • 触发$viewContentLoaded
    • 如果提供onload属性,调用所指定的函数

路由

  • 使用$routeProvider的when和otherwise
1
2
3
4
5
6
angular.module('myApp', []).config(function($routeProvider) {
$routeProvider
.when('/',{
// controller、template、templateURL、resolve、redirectTo、reloadOnSearch和css
})
})

controller 字符串或函数

template html模板,templateURL模板链接

resolve 将resolve对象里的值注入到控制器中

redirectTo 路由替换,字符串或者函数,在调用函数时会传入:从当前路径中提取的路由参数,当前路径,当前URL中的查询串

reloadOnSearch true时, $location.search()变化时会重新加载路由

路由模式

  • 标签模式

$location服务

类window.location,能修改路径和内部跳转,不能刷新整个页面

  • $location.path() 获取或修改路径,并且与HTML5的历史api交互,提供用户后退的功能,加replace(),就不能后退
  • absUrl() 获取编码后完整的URL
  • hash() 获取URL中的hash片段
  • host() 获取URL中的主机,port()端口号,
  • protocol() 获取URL中的协议
  • search() 获取URL中的查询串,也可以用对象、字符串设置查询
  • url() 获取或修改当前的URL

事件

ng-click 定义了 AngularJS 点击事件。

1
2
3
4
5
6
7
< div ng-app="" ng-controller="myCtrl">
< button ng-click="count = count + 1">点我!</button>
< p>{{ count }}</p>
< /div>

ng-show 显示 HTML 元素
ng-hide 隐藏 HTML 元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
< div ng-app="myApp" ng-controller="personCtrl">
<button ng-click="toggle()">>隐藏/显示</button>
< p ng-hide="myVar">
名: <input type="text" ng-model="firstName"><br>
姓名: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}
</p>
</div>
< script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
$scope.firstName = "John",
$scope.lastName = "Doe"
$scope.myVar = false;
$scope.toggle = function() {
$scope.myVar = !$scope.myVar;
};
});
</script>

模块 angular.module

1
2
3
4
5
6
7
8
< div ng-app="myApp">...</div>
< script>
//在模块定义中 [] 参数用于定义模块的依赖关系。中括号[]表示该模块没有依赖,如果有依赖的话会在中括号写上依赖的模块名字。
var app = angular.module("myApp", []);
</script>

添加控制器 用 ng-controller 指令来添加应用的控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
< div ng-app="myApp" ng-controller="myCtrl">
{{ firstName + " " + lastName }}
</div>
< script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
});
</script>

表单

  • 输入控件的集合
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
< div ng-app="myApp" ng-controller="formCtrl">
//novalidate 属性是在 HTML5 中新增的。禁用了使用浏览器的默认验证
<form novalidate>
First Name:<br>
//ng-model 指令绑定了两个 input 元素到模型的 user 对象
<input type="text" ng-model="user.firstName"><br>
Last Name:<br>
<input type="text" ng-model="user.lastName">
<br><br>
//ng-click 指令调用了 reset() 方法,且在点击按钮时调用
<button ng-click="reset()">RESET</button>
</form>
<p>form = {{user}}</p>
<p>master = {{master}}</p>
</div>
< script>
var app = angular.module('myApp', []);
//formCtrl 函数设置了 master 对象的初始值,并定义了 reset() 方法
app.controller('formCtrl', function($scope) {
$scope.master = {firstName: "John", lastName: "Doe"};
$scope.reset = function() {
$scope.user = angular.copy($scope.master);
};
$scope.reset();
});
</script>

输入验证

  • 表单和控件可以提供验证功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
< form ng-app="myApp" ng-controller="validateCtrl"
name="myForm" novalidate>
< p>用户名:<br>
< input type="text" name="user" ng-model="user" required>
< span style="color:red" ng-show="myForm.user.$dirty && myForm.user.$invalid">
< span ng-show="myForm.user.$error.required">用户名是必须的。</span>
</span>
</p>
< p>邮箱:<br>
< input type="email" name="email" ng-model="email" required>
< span style="color:red" ng-show="myForm.email.$dirty && myForm.email.$invalid">
< span ng-show="myForm.email.$error.required">邮箱是必须的。</span>
< span ng-show="myForm.email.$error.email">非法的邮箱。</span>
</span>
< /p>
< p>
< input type="submit"
ng-disabled="myForm.user.$dirty && myForm.user.$invalid ||
myForm.email.$dirty && myForm.email.$invalid">
</p>
< /form>
< script>
var app = angular.module('myApp', []);
app.controller('validateCtrl', function($scope) {
$scope.user = 'John Doe';
$scope.email = 'john.doe@gmail.com';
});
< /script>

API

  • 比较、迭代、转换
1
2
3
4
angular.lowercase()
angular.uppercase()
angular.isString()
angular.isNumber()

包含 ng-include

  • 可以在 HTML 中包含 HTML 文件
1
2
3
4
5
6
7
8
< body ng-app="myApp" ng-controller="userCtrl">
< div class="container">
<div ng-include="'myUsers_List.htm'"></div>
<div ng-include="'myUsers_Form.htm'"></div>
< /div>
< /body>

动画 angular-animate.min.js ngAnimate

ngAnimate 监测事件,添加或移除 class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
< body ng-app="myApp">
< h1>隐藏 DIV: <input type="checkbox" ng-model="myCheck"></h1>
< div ng-hide="myCheck"></div>
< script>
var app = angular.module('myApp', ['ngAnimate']);
</script>
< style>
@keyframes myChange {
from {
height: 100px;
} to {
height: 0;
}
}
div {
height: 100px;
background-color: lightblue;
}
div.ng-hide {
animation: 0.5s myChange;
}
</style>

过滤器

  • 使用一个管道字符(|)添加到表达式和指令中

    currency 格式化数字为货币格式

    1
    2
    3
    4
    5
    6
    7
    8
    < div ng-app="myApp" ng-controller="costCtrl">
    < input type="number" ng-model="quantity">
    < input type="number" ng-model="price">
    < p>总价 = {{ (quantity * price) | currency }}</p>
    < /div>

filter 从数组项中选择一个子集

1
2
3
4
5
6
7
8
9
10
11
< div ng-app="myApp" ng-controller="namesCtrl">
< p><input type="text" ng-model="test"></p>
<ul>
<li ng-repeat="x in names | filter:test | orderBy:'country'">
{{ (x.name | uppercase) + ', ' + x.country }}
</li>
</ul>
</div>

lowercase 格式化字符串为小写

1
2
3
4
5
< div ng-app="myApp" ng-controller="personCtrl">
< p>姓名为 {{ lastName | lowercase }}</p>
</div>

orderBy 根据某个表达式排列数组

1
2
3
4
5
6
7
8
9
10
< div ng-app="myApp" ng-controller="namesCtrl">
< ul>
<li ng-repeat="x in names | orderBy:'country'">
{{ x.name + ', ' + x.country }}
</li>
</ul>
< div>

uppercase 格式化字符串为大写

1
2
3
4
5
< div ng-app="myApp" ng-controller="personCtrl">
< p>姓名为 {{ lastName | uppercase }}</p>
< /div>

服务(Service)

  • 服务是一个函数或对象
  • 会一直监控应用,处理事件变化

$location 返回当前页面的 URL 地址, $location服务是作为一个参数传递到controller中,使用时需要在 controller中定义,使用 $location 服务比使用 window.location 对象更好

1
2
3
4
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $location) {
myUrl = $location.absUrl();
});

$http 向服务器发送请求,应用响应服务器传送过来的数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//使用 $http 服务向服务器请求数据
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
//$http.get() 从web服务器上读取静态 JSON 数据
$http.get("welcome.htm").then(function (response) {
$scope.myWelcome = response.data;
});
});
//json
< div ng-app="myApp" ng-controller="customersCtrl">
< ul>
< li ng-repeat="x in names">
{{ x.Name + ', ' + x.Country }}
</li>
</ul>
< /div>
< script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("http://www.runoob.com/try/angularjs/data/Customers_JSON.php")
.success(function(response) {$scope.names = response.records;});
//当从服务端载入 JSON 数据时,$scope.names 变为一个数组
});
< /script>

$timeout 对应了 JS window.setTimeout 函数

1
2
3
4
5
6
7
8
// 两秒后显示信息
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $timeout) {
$scope.myHeader = "Hello World!";
$timeout(function () {
$scope.myHeader = "How are you today?";
}, 2000);
});

$interval 对应了 JS window.setInterval 函数

1
2
3
4
5
6
7
8
//每两秒显示信息:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $interval) {
$scope.theTime = new Date().toLocaleTimeString();
$interval(function () {
$scope.theTime = new Date().toLocaleTimeString();
}, 1000);
});

创建自定义服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
创建名为hexafy 的访问:
app.service('hexafy', function() {
this.myFunc = function (x) {
return x.toString(16);
}
});
使用自定义的的服务 hexafy 将一个数字转换为16进制数:
app.controller('myCtrl', function($scope, hexafy) {
$scope.hex = hexafy.myFunc(255);
});
在过滤器 myFormat 中使用服务 hexafy:
app.filter('myFormat',['hexify', function(hexify) {
return function(x) {
return hexify.myFunc(x);
};
}]);
创建服务 hexafy:
< ul>
<li ng-repeat="x in counts">{{x | myFormat}}</li>
</ul>

Select(选择框) ng-options

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
< div ng-app="myApp" ng-controller="myCtrl">
< select ng-model="selectedName" ng-options="x for x in names">
</select>
< /div>
< script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.names = ["Google", "Runoob", "Taobao"];
});
</script>
//也可以用 ng-repeat,通过数组来循环 HTML 代码来创建下拉列表
<select>
<option ng-repeat="x in names">{{x}}</option>
</select>

ng-options 指令更适合创建下拉列表,使用 ng-options 的选项是一个对象, ng-repeat 是一个字符串

1
2
3
4
5
6
7
8
9
10
11
使用 ng-repeat:
< select ng-model="selectedSite">
< option ng-repeat="x in sites" value="{{x.site}}">{{x.site}}</option>
</select>
< h1>你选择的是: {{selectedSite}}</h1>
使用 ng-options:
< select ng-model="selectedSite" ng-options="x.site for x in sites">
</select>
< h1>你选择的是: {{selectedSite.site}}</h1>
< p>网址为: {{selectedSite.url}}</p>

ng-options 操作对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
< div ng-app="myApp" ng-controller="myCtrl">
< p>选择一辆车:</p>
< select ng-model="selectedCar" ng-options="x for (x, y) in cars">
</select>
直接使用对象的属性
//<select ng-model="selectedCar" ng-options="y.brand for (x, y) in cars">
//</select>
< h1>你选择的是: {{selectedCar.brand}}</h1>
< h2>模型: {{selectedCar.model}}</h2>
< h3>颜色: {{selectedCar.color}}</h3>
< p>注意选中的值是一个对象。</p>
</div>
< script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.cars = {
car01 : {brand : "Ford", model : "Mustang", color : "red"},
car02 : {brand : "Fiat", model : "500", color : "white"},
car03 : {brand : "Volvo", model : "XC90", color : "black"}
}
});
</script>

表格

  • 表格显示序号可以在 中添加 $index:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
< div ng-app="myApp" ng-controller="customersCtrl">
< table>
<tr ng-repeat="x in names">
<td>{{ x.Name }}</td>
<td>{{ x.Country }}</td>
</tr>
</table>
</div>
< script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("http://www.runoob.com/try/angularjs/data/Customers_JSON.php")
.success(function (response) {$scope.names = response.records;});
});
</script>

使用 $even 和 $odd

1
2
3
4
5
6
7
8
< table>
<tr ng-repeat="x in names">
<td ng-if="$odd" style="background-color:#f1f1f1">{{ x.Name }}</td>
<td ng-if="$even">{{ x.Name }}</td>
<td ng-if="$odd" style="background-color:#f1f1f1">{{ x.Country }}</td>
<td ng-if="$even">{{ x.Country }}</td>
</tr>
</table>

HTML DOM

ng-disabled 指令直接绑定应用程序数据到 HTML 的 disabled 属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
< div ng-app="" ng-init="mySwitch=true">
< p>
< button ng-disabled="mySwitch">点我!</button>
</p>
< p>
< input type="checkbox" ng-model="mySwitch">按钮
</p>
< p>
{{ mySwitch }}
</p>
< /div>

ng-show 指令隐藏或显示一个 HTML 元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
< div ng-app="">
< p ng-show="true">我是可见的。</p>
< p ng-show="false">我是不可见的。</p>
</div>
使用表达式来计算布尔值( true 或 false)
< div ng-app="">
< p ng-show="hour > 12">我是可见的。</p>
< /div>

ng-hide 指令用于隐藏或显示 HTML 元素

AngularJS 应用组成如下:

View(视图), 即 HTML。
Model(模型), 当前视图中可用的数据。
Controller(控制器), 即 JavaScript 函数,可以添加或修改属性。

表达式

  • 使用 表达式 把数据绑定到 HTML
  • 表达式写在双大括号内:,类似ng-bind
1
2
3
4
5
6
7
< div ng-app="" ng-init="quantity=1;cost=5">
< p>总价: {{ quantity * cost }}</p>
</div>
< div ng-app="" ng-init="quantity=1;cost=5">
< p>总价: <span ng-bind="quantity * cost"></span></p>
< /div>

指令

ng-app 指令初始化一个 AngularJS 应用程序,定义根元素,在网页加载完毕时会自动引导应用程序

ng-init 指令为 AngularJS 应用程序定义了 初始值,通常情况下,不使用 ng-init,用控制器或模块来代替它

ng-model 指令把元素值(比如输入域的值)绑定到应用程序,提供类型验证(number、email、required),提供状态(invalid、dirty、touched、error),为 HTML 元素提供 CSS 类。绑定 HTML 元素到 HTML 表单。

1
2
3
4
5
6
7
< div ng-app="" ng-init="firstName='John'">
<p>在输入框中尝试输入:</p>
<p>姓名:<input type="text" ng-model="firstName"></p>
<p>你输入的为: {{ firstName }}</p> //AngularJS 数据绑定表达式
</div>

ng-repeat 指令对于集合中(数组中)的每个项会 克隆一次 HTML 元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
< div ng-app="" ng-init="names=['Jani','Hege','Kai']">
< p>使用 ng-repeat 来循环数组</p>
< ul>
< li ng-repeat="x in names">
{{ x }}
< /li>
< /ul>
< div>
< div ng-app="" ng-init="names=[
{name:'Jani',country:'Norway'},
{name:'Hege',country:'Sweden'},
{name:'Kai',country:'Denmark'}]">
< p>循环对象:</p>
< ul>
<li ng-repeat="x in names">
{{ x.name + ', ' + x.country }}
</li>
</ul>
< /div>

创建自定义的指令

使用 .directive 函数来添加自定义的指令

使用驼峰法来命名一个指令, runoobDirective, 但在使用它时需要以 - 分割, runoob-directive:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<body ng-app="myApp">
< runoob-directive></runoob-directive>
< script>
var app = angular.module("myApp", []);
app.directive("runoobDirective", function() {
return {
template : "< h1>自定义指令!</h1>"
};
});
< /script>
< /body>

以通过以下方式来调用指令:

1
2
3
4
元素名 <runoob-directive></runoob-directive>
属性 <div runoob-directive></div>
类名 <div class="runoob-directive"></div>
注释 <!-- 指令: runoob-directive -->

限制你的指令只能通过特定的方式来调用

1
2
3
4
5
6
7
8
var app = angular.module("myApp", []);
app.directive("runoobDirective", function() {
return {
restrict : "A",// E 只限元素名使用, A 只限属性使用, C 只限类名使用,M 只限注释使用,restrict 默认值为 EA
//对于注释 要添加 replace:true 属性
template : "<h1>自定义指令!</h1>"
};
});

模型 ng-model

  • 将输入域的值与 AngularJS 创建的变量绑定
1
2
3
4
5
6
7
8
9
10
< div ng-app="myApp" ng-controller="myCtrl">
名字: <input ng-model="name">
< /div>
< script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.name = "John Doe";
});
< /script>
  • 双向绑定
1
2
3
4
< div ng-app="myApp" ng-controller="myCtrl">
名字: <input ng-model="name">
<h1>你输入了: {{name}}</h1>
</div>
  • 验证用户输入
1
2
3
4
5
<form ng-app="" name="myForm">
Email:
<input type="email" name="myAddress" ng-model="text">
<span ng-show="myForm.myAddress.$error.email">不是一个合法的邮箱地址</span>
</form>
  • 应用状态(invalid合法, dirty改变, touched触屏点击, error)
1
2
3
4
5
6
7
8
< form ng-app="" name="myForm" ng-init="myText = 'test@runoob.com'">
Email:
<input type="email" name="myAddress" ng-model="myText" required></p>
<h1>状态</h1>
< p>Valid: {{myForm.myAddress.$valid}} (如果输入的值是合法的则为 true)。</p>
< p>Dirty: {{myForm.myAddress.$dirty}} (如果值改变则为 true)。</p>
< p>Touched: {{myForm.myAddress.$touched}} (如果通过触屏点击则为 true)。</p>
< /form>
  • CSS 类

可用状态:ng-empty ,ng-not-empty ,ng-touched ,ng-untouched ,ng-valid ,ng-invalid ,ng-dirty ,ng-pending ,ng-pristine

1
2
3
4
5
6
7
8
9
10
11
12
13
<style>
input.ng-invalid {
background-color: lightblue;
}
</style>
<body>
< form ng-app="" name="myForm">
输入你的名字:
<input name="myAddress" ng-model="text" required>
< /form>
< p>编辑文本域,不同状态背景颜色会发送变化。</p>
< p>文本域添加了 required 属性,该值是必须的,如果为空则是不合法的。</p>

Scope(作用域)

  • scope 是模型
  • Scope(作用域) 是应用在 HTML (视图) 和 JavaScript (控制器)之间的纽带,是一个对象,有可用的方法和属性,可应用在视图和控制器上

可以将 $scope 对象当作一个参数传递,当在控制器中添加 $scope 对象时,视图 (HTML) 可以获取了这些属性:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
< div ng-app="myApp" ng-controller="myCtrl">
< h1>{{carname}}</h1>
</div>
< script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.carname = "Volvo";
});
< /script>

根作用域 所有的应用都有一个 $rootScope,它可以作用在 ng-app 指令包含的所有 HTML 元素中,是各个 controller 中 scope 的桥梁,用 rootscope 定义的值,可以在各个 controller 中使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
< div ng-app="myApp" ng-controller="myCtrl">
< h1>{{lastname}} 家族成员:</h1>
< ul>
<li ng-repeat="x in names">{{x}} {{lastname}}
</ul>
< /div>
< script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $rootScope) {
$scope.names = ["Emil", "Tobias", "Linus"];
$rootScope.lastname = "Refsnes";
});
</script>

控制器 ng-controller

  • 控制 AngularJS 应用程序的数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//定义AngularJS 应用程序 定义控制器
< div ng-app="myApp" ng-controller="myCtrl" >
//ng-model 指令绑定输入域到控制器的属性(firstName 和 lastName)
名: <input type="text" ng-model="firstName"><br>
名: <input type="text" ng-model="lastName"><br>
<br>
姓名: {{firstName + " " + lastName}}
< /div>
< script>
var app = angular.module('myApp', []);
//$scope (相当于作用域、控制范围)用来保存AngularJS Model(模型)的对象
app.controller('myCtrl', function($scope) {
//控制器在作用域中创建了两个属性 (firstName 和 lastName)
$scope.firstName = "John";
$scope.lastName = "Doe";
});
< /script>

控制器方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
< div ng-app="myApp" ng-controller="personCtrl">
名: <input type="text" ng-model="firstName"><br>
姓: <input type="text" ng-model="lastName"><br>
<br>
姓名: {{fullName()}}
</div>
< script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
$scope.fullName = function() {
return $scope.firstName + " " + $scope.lastName;
}
});
</script>

外部文件中的控制器 只需要把 < script> 标签中的代码复制到名为 personController.js 的外部文件中即可:

1
2
3
4
5
6
7
8
9
10
< div ng-app="myApp" ng-controller="personCtrl">
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}
< /div>
< script src="personController.js"></script>

关于URL编码的问题

encodeURI,decodeURI :不会对下列字符编码 ASCII字母 数字 ~!@#$&*()=:/,;?+’,适合对整个url

encodeURIComponent decodeURIComponent : 不会对下列字符编码 ASCII字母 数字 ~!*()’,适合对url参数

content-type

application/x-www-form-urlencoded: body的内容为a=b&c=d,服务端接收时会转为键值对并会把+转成空格,需要对值做encodeURIComponent
application/json: body的内容为JSON.stringify({a:1}})

字符串编码

escape unescape :不会对 ASCII 字母、数字和@/*+进行编码,但已不建议使用

escape和encodeURI区别 :
escape在处理 0xff 之外字符的时候,是直接使用字符的 unicode 在前面加上一个 「%u」
encodeURI 先进行 UTF-8,再在 UTF-8 的每个字节码前加上一个 「%」

nodejs 处理编码文件

iconv-lite: 纯js实现,少了c++到js的转换 比node-iconv快

1
2
3
4
5
6
7
8
9
10
var iconv = require('iconv-lite');
// Convert from an encoded buffer to js string.
str = iconv.decode(buf, 'win1251');
// Convert from js string to an encoded buffer.
buf = iconv.encode("Sample input string", 'win1251');
// Check if encoding is supported
iconv.encodingExists("us-ascii")

encoding: 对node-iconv和iconv-lite的再次封装,encoding首先调用node-iconv,如果node-iconv无法解析,则调用iconv-lite作为替代方案.
encoding模块就一个方法convert(),使用方法为:encoding.convert(text, toCharset, fromCharset)。

  • text: 需要转换的对象,可以为Buffer或者String对象。
  • toCharset: 转换后的编码。
  • fromCharset: 转换前的编码,缺省为uft8。

转换后的输入结果为Buffer对象。

URL、URI、URN

URI:统一资源标识符

URL:统一资源定位器,具体的URI

URN:统一资源命名,如mailto:java-net@java.sun.com

选择系统版本

  • Ubuntu server

系统安装步骤

  • 其他不变,注意在安装软件时选中Samba Server

配置

  • 配置文件在 /etc/samba/samba.conf
    • 如需要权限编辑:chmod 777 .
  • 修改[profiles],它在最后几行
    • 取消注释
    • guest ok =yes//允许匿名登录
    • browseable=yes//允许浏览
    • readonly=no // 可写
    • path//共享文件存放路径,可能还不存在,创建(mkdir,记得开放权限)

启动服务

  • sudo smbd

连接

mac :命令 open smb://ip address

windows :运行 //ip address

diskutil list

  • 查看U盘位置 如:/dev/disk1

diskutil unmountDisk /dev/disk2

  • 取消挂载

sudo dd if=**.iso of=/dev/rdisk1 bs=1m;sync

  • 写入文件

Webpack开发服务器(webpack-dev-server)

  • npm install -g webpack-dev-server
  • 静态资源Web服务器
  • 基于Node.js Express
  • 内容实时打包和推送

react-hot-loader

  • npm install —save-dev react-hot-loader
  • React组件的热替换

创建一个server.js文件,用于启动webpack-dev-server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var config = require('../webpack.config');
new WebpackDevServer(webpack(config), {
publicPath: config.output.publicPath,
hot: true,//开启Hot Module Replacement (HMR)
noInfo: false,
historyApiFallback: true
}).listen(3000, '127.0.0.1', function (err, result) {
if (err) {
console.log(err);
}
console.log('Listening at localhost:3000');
});

修改webpack.config.js

1
2
3
4
5
6
7
8
9
10
11
12
entry: [
'webpack-dev-server/client?http://127.0.0.1:3000', // WebpackDevServer host and port,资源服务器地址,打包生成的文件从client?http://127.0.0.1:3000去获取动态的代码更新
'webpack/hot/only-dev-server',
'./scripts/index.js' // Your appʼs entry point
]
loaders: [{
test: /\.js$/,
exclude: /node_modules/,
loader: 'react-hot!jsx-loader?harmony'//让Webpack用react-hot-loader去加载React组件,harmony启动es6
}]

之后启动node server.js

前端服务器调用

载入打包文件时指定完整的URL地址

1
<script src="http://127.0.0.1:3000/assets/bundle.js"></script>

建立Socket IO来动态加载模块