第二章
- 实现计数器
<body ng-app>
<input type="number" ng-model="value"/>
<input type="button" ng-click="value=value+1" value="增加"/>
</body>
- 模块和控制器和表达式
<body ng-app="app">
<div ng-controller="mainController">
<span>{{name}}</span>
<button ng-click='say()'>登录</button>
{{ fn() +5 }}
{{ arr[2] }}
</div>
<script src="lib\angular.js"></script>
<script>
var app = angular.module('app',[]);
app.controller('mainController',function($scope){
$scope.name = "我是属性name的值";
$scope.say = function() {
$scope.welcome= 'welcome:' + $scope.name + '!';
};
$scope.fn = function () {
return 1;
};
});
</script>
</body>
- 模块间的相互依赖
<div ng-app="demo">
<div ng-controller="MainController">
<span>{{name}}</span>
</div>
</div>
<script src="lib/angular.js"></script>
<script>
var ctrls = angular.module('ctrls',[]);
ctrls.controller('MainController', function ($scope) {
$scope.name = "张三";
});
var demo = angular.module('demo', ['ctrls']);
</script>
- 根作用域【$rootScope】
<body>
<div ng-app="demo">
<p>*ng-app="demo"作用域显示结果:</p>
<span>{{rData}}</span>
<span>{{data}}</span>
<div ng-controller="MainController">
<p>*ng-controller="MainController"作用域显示结果:</p>
<span>{{rData}}</span>
<span>{{data}}</span>
</div>
</div>
<script src="lib/angular.js"></script>
<script>
var demo = angular.module('demo', []);
demo.controller('MainController', function ($scope, $rootScope) {
$rootScope.rData = 'hello rootScope';
$scope.data = 'hello scope';
})
</script>
</body>
- 作用域的嵌套
<body>
<div ng-app="demo">
<div ng-controller="MainController">
<p>*ng-controller="MainController"作用域显示结果:</p>
<span>{{mData}}</span>
<span>{{dData}}</span>
<div ng-controller="DataController">
<p>*ng-controller="DataController"作用域显示结果:</p>
<span>{{mData}}</span>
<span>{{dData}}</span>
</div>
</div>
</div>
<script src="lib/angular.js"></script>
<script>
var demo = angular.module('demo', []);
demo.controller('MainController', function ($scope) {
$scope.mData = "hello MainController";
});
demo.controller('DataController', function ($scope) {
$scope.dData= "hello DataController";
});
</script>
</body>
- 双向数据绑定
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>AngularJS双向绑定</title>
</head>
<body ng-app>
请输入内容:<input type="text" ng-model="demo"/><br/>
您输入的内容:{{demo}}
<script src="lib/angular.js"></script>
</body>
</html>
第三章
常用内置指令
- 框架
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<div ng-app="app">
<div ng-controller="MainController">
...
</div>
</div>
<script src="lib/angular.js"></script>
<script>
var app = angular.module('app', []);
app.controller('MainController', function ($scope) {
$scope.XXX = 'XXX';
});
</script>
</body>
</html>
- 插值语法和ng-bind
<span>{{data}}</span>
<span ng-bind="data"></span>
- 作用域样式属性
<div style="height: 100px;" ng-style="myStyle"></div>
- 绑定样式
// 在style标签里定义样式 .myColor .myDiv .myBorder
<div style="height: 100px" ng-class="classOptions"></div>
$scope.classOptions ={
myColor:true,
myDiv:true,
"myBorder":true
// "myBorder":false
}
- ng-show和ng-hide
<div style="width: 200px;height: 100px;background: red" ng-show="myTrue">
<div style="width: 200px;height: 100px;background:blue" ng-hide="myTrue">
$scope.myTrue=true;
$scope.myFalse=false;
- ng-readonly【可用】和 on-disabled【禁用】
<input name="id1" type="text" ng-model="text" ng-readonly="boolean">
<input name="id2" type="text" ng-model="text" ng-disabled="boolean">
$scope.boolean = true;
- ng-click
<button ng-click="clickHandler()" onclick="onclickHandler()">按钮</button>
$scope.clickHandler = function () {
$scope.message = 'ng-click事件被触发。'
}
function onclickHandler() {
console.log("onclick事件被触发");
}
- ng-change
<input type="text" ng-change="myFunc()" ng-model="myValue" />
$scope.myFunc = function() {
$scope.message="输入框被修改了";
};
- ng-if
<input type="checkbox" ng-model="myVar" ng-init="myVar=false">
<div ng-if="myVar">
<h1>我来了</h1>
</div>
- ng-switch
<div ng-switch="name">
<div ng-switch-when="张三">
this is 张三
</div>
<div ng-switch-when="李四">
this is 李四
</div>
<div ng-switch-default>
找不到此人
</div>
</div>
$scope.name = "李四"
- ng-repeat
<table style="border: solid 1px">
<tr ng-repeat="item in users">
<td>{{item.name}}</td>
<td>{{item.sex}}</td>
<td>{{item.age}}</td>
<td>index:{{$index}}, first:{{$first}}, last:{{$last}},
middle:{{$middle}}, even:{{$even}}, odd:{{$odd}}</td>
</tr>
</table>
$scope.users = [
{name: "张三", sex: "男", age: 24},
{name: "李四", sex: "女", age: 13},
{name: "王五", sex: "男", age: 38},
{name: "赵六", sex: "女", age: 55}
]
// ——————————————————————————————————————————————————————————————————————————————
<tr ng-repeat="item in numbers track by $index" >
<td>{{item}}</td>
<td>$index:{{$index}}</td>
</tr>
$scope.numbers = [1,1,4,2,5];
// ——————————————————————————————————————————————————————————————————————————————
<tr ng-repeat="(key,value) in users">
<td>{{key}}</td>
<td>{{value.sex}}</td>
<td>{{value.age}}</td>
</tr>
$scope.users = {
"张三": {age: 24, sex: "男"},
"李四": {age: 13, sex: "女"},
"王五": {age: 38, sex: "男"},
"赵六": {age: 55, sex: "女"}
}
自定义指令
- 指令的作用域
- 基本框架
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<div ng-app="app">
<div><my-say-hello></my-say-hello></div>
</div>
<script src="lib/angular.js"></script>
<script>
var app = angular.module('app', []);
app.directive('mySayHello', function () {
return {
template: "<p>hello world</p>",
replace:false
}
});
</script>
</body>
</html>
- 指令的作用域
<div ng-app="app">
<div ng-init="myProperty='by-parent'">
{{ myProperty }}
<div my-directive ng-init="myProperty='by my-child'">
{{ myProperty }}
</div>
</div>
</div>
<script src="lib/angular.js"></script>
<script>
var app = angular.module('app', []);
app.directive('myDirective', function() {
return {
scope:true
};
})
</script>
- 独立作用域
<body>
<div ng-app="app">
<div ng-controller = 'insulate'>
<user-info user-name="小明" age='num' speak='sayHello(name)'></user-info>
</div>
</div>
<script src="lib/angular.js"></script>
<script>
var app = angular.module('app', []);
app.controller('insulate',function($scope){
$scope.num = 17;
$scope.sayHello=function(name){
alert("Hello "+name);
}
})
app.directive('userInfo',function(){
return{
restrict:'EA',
template:'姓名:<input type="text" ng-model="userName" /><br/>'+
'年龄: {{age}} <br/>'+
'<button class="btn btn-default" ng-click="speak({name:userName})">speaking</button><br/>' ,
scope:{
userName:'@',
age:'=',
speak:'&'
}
}
});
</script>
</body>
第四章
- 作用域上的数据监听
<input type="text" ng-model="data"/>
$scope.$watch('data', function (newValue, oldValue) {
console.log('data has changed', ',', newValue, ',', oldValue);
});
- $apply() 函数
<div ng-app="demo">
<div ng-controller="MainController">
<input type="number" ng-model="data"/>
<button onclick="plusOne()">加1</button>
</div>
</div>
<script src="lib/angular.js"></script>
<script>
var demo = angular.module('demo', []);
demo.controller('MainController', function ($rootScope, $scope) {
$scope.data = 1;
window.scope = $scope;
});
function plusOne() {
window.scope.data += 1;
window.scope.$apply();
}
</script>
- 依赖注入的三种方式
<body ng-app="myApp">
<div ng-controller="MyController">
<button ng-click="sayHello()">调用sayHello</button>
</div>
</body>
<script src="lib/angular.js"></script>
<script>
var app=angular.module('myApp', []);
app.value("temp", "Hello");
app.controller('MyController',function($scope,temp) {
$scope.sayHello = function() {
alert(temp);
}
});
</script>
第五章
创建服务
- 使用Provider创建服务
<script>
var app = angular.module('app', []);
var myFunction=function () {
var name = 'world';
return {
$get: function () {
return {
msg: 'hello ' + name
}
},
changeName: function (newName) {
name = newName;
}
}
}
app.provider('myService', myFunction);
app.config(function (myServiceProvider) {
myServiceProvider.changeName('张三');
});
app.controller('MainController', function ($scope, myService) {
$scope.data = myService.msg;
});
</script>
- 使用Factory创建服务
<script>
var app = angular.module('app', []);
var myFunction=function(){
return {
msg: "this is data"
}
}
app.factory('myService', myFunction);
app.controller('MainController', function ($scope, myService) {
$scope.data = myService.msg;
});
</script>
- 使用Service创建服务
<script>
var app = angular.module('app',[]);
app.service('myService',function(){
this.sayHello = function(name) {
alert('hello ' + name)
}
});
app.controller('MainController',function($scope,myService){
$scope.say=function(name){
myService.sayHello(name);
}
});
</script>
- 使用 constant 配置服务
<script>
var app = angular.module('app',[]);
app.provider('myService', function () {
var name = 'world';
return {
$get: function () {
return {
msg: 'hello ' + name
}
},
changeName: function (newName) {
name = newName;
}
}
});
app.constant('country','China');
app.config(function(myServiceProvider,country){
myServiceProvider.changeName(country)
});
app.controller('MainController', function ($scope, myService) {
$scope.data = myService.msg;
});
</script>
服务类型
- $window
<script>
var app = angular.module('myApp', []);
app.controller('MainController', function($scope, $window) {
$scope.displayAlert = function (msg) {
$window.alert(msg);
}
});
</script>
- $location
<script>
var app = angular.module('myApp', []);
app.controller('MainController', function($scope, $location) {
$scope.host_Url = $location.host();
$scope.port_Url = $location.port();
$scope.protocol_Url = $location.protocol();
$scope.hash_Url = $location.hash('myhash').hash();
$scope.path_Url = $location.path('/newPath').path();
$scope.search_Url = $location.search('name','lily').search();
$scope.url_Url = $location.url();
$scope.abs_Url = $location.absUrl();
});
</script>
-
w
a
t
c
h
监
听
watch监听
watch监听location
var app = angular.module('app', []);
app.controller('MainController', function ($scope, $location) {
$scope.locat = $location;
// $watch监听的是AngularJS的表达式。
$scope.$watch('locat.path()', function (newValue, oldValue) {
console.log(newValue);
});
})
- $http服务
var app = angular.module('app', []);
app.controller('MainController', function ($scope, $http) {
$http({
method: 'GET',
url: 'data.json'
}).then(function successCallback(response) {
$scope.msg = response.data.msg;
}, function errorCallback(response) {
console.log(response.data)
console.log(response.status)
});
});
路由
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>AngularJS路由</title>
</head>
<body>
<div ng-app="app">
<a href="#!/index">index</a>
<a href="#!/first">demo5-7</a>
<a href="#!/about/2/lucy">lucy</a>
<a href="#!/about/3/banny">banny</a>
<div ng-view></div>
</div>
<script src="lib/angular.js"></script>
<script src="lib/angular-route.js"></script>
<script>
var app = angular.module('app', ['ngRoute']);
app.config(function ($routeProvider) {
$routeProvider
.when('/index', {
template: '<H2>{{name}}</H2>',
controller: function ($scope) {
$scope.name = 'index';
}
})
.when('/first', {
templateUrl:'demo5-7.html'
})
.when('/about/:id/:name', {
template: '<H2>{{params}}</H2>',
controller: function ($scope, $routeParams) {
$scope.params=$routeParams
}
})
.otherwise({
redirectTo: '/index'
});
});
</script>
</body>
</html>
|