AngularJS 是一个开源的JavaScript框架,用于构建动态Web应用程序。它通过扩展HTML的语法,让开发者能够以声明式的方式构建用户界面,并通过双向数据绑定自动同步视图与数据模型。
npm install angular
yarn add angular
bower install angular
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="angular.js"></script>
</head>
<body>
<div ng-controller="MyController">
<input ng-model="name" placeholder="请输入姓名">
<h1>Hello {{name}}!</h1>
</div>
<script>
angular.module('myApp', [])
.controller('MyController', function($scope) {
$scope.name = 'World';
});
</script>
</body>
</html>
// 定义模块
var app = angular.module('myApp', []);
// 创建控制器
app.controller('MyController', function($scope) {
$scope.users = [
{ name: 'Alice', email: 'alice@example.com' },
{ name: 'Bob', email: 'bob@example.com' }
];
$scope.addUser = function(user) {
$scope.users.push(user);
$scope.newUser = {};
};
});
<div ng-controller="MyController">
<input type="text" ng-model="newUser.name" placeholder="姓名">
<input type="email" ng-model="newUser.email" placeholder="邮箱">
<button ng-click="addUser(newUser)">添加用户</button>
<ul>
<li ng-repeat="user in users">
{{user.name}} - {{user.email}}
</li>
</ul>
</div>
app.factory('UserService', function($http) {
return {
getUsers: function() {
return $http.get('/api/users');
},
createUser: function(user) {
return $http.post('/api/users', user);
}
};
});
app.controller('UserController', function($scope, UserService) {
UserService.getUsers().then(function(response) {
$scope.users = response.data;
});
});
// 自动依赖注入
angular.module('app', [])
.controller('MyCtrl', ['$scope', '$http', function($scope, $http) {
// 控制器逻辑
$http.get('/api/data').then(function(response) {
$scope.data = response.data;
});
}]);
// 自定义服务
angular.module('app')
.service('DataService', function($http) {
this.fetchData = function() {
return $http.get('/api/data');
};
});
// 自定义指令
angular.module('app')
.directive('myDirective', function() {
return {
restrict: 'EA',
scope: {
data: '=',
onAction: '&'
},
templateUrl: 'my-directive.html',
link: function(scope, element, attrs) {
scope.handleClick = function() {
scope.onAction({value: scope.data});
};
}
};
});
// 使用自定义指令
<my-directive data="user" on-action="handleAction(value)"></my-directive>
// 简化的$watch实现
Scope.prototype.$watch = function(watchFn, listenerFn) {
var watcher = {
watchFn: watchFn,
listenerFn: listenerFn || function() {},
last: initWatchVal
};
this.$$watchers.push(watcher);
};
// 简化的$digest循环
Scope.prototype.$digest = function() {
var dirty;
var ttl = 10;
do {
dirty = false;
for (var i = 0; i < this.$$watchers.length; i++) {
var watcher = this.$$watchers[i];
var newValue = watcher.watchFn(this);
var oldValue = watcher.last;
if (!this.$$areEqual(newValue, oldValue, watcher.valueEq)) {
watcher.last = (watcher.valueEq ? copy(newValue) : newValue);
watcher.listenerFn(newValue,
(oldValue === initWatchVal ? newValue : oldValue),
this);
dirty = true;
}
}
} while (dirty && ttl--);
};
// 自定义过滤器
angular.module('app')
.filter('capitalize', function() {
return function(input) {
if (input) {
return input.charAt(0).toUpperCase() + input.slice(1);
}
return input;
};
});
// 内置过滤器使用
{{ name | uppercase }}
{{ price | currency }}
{{ date | date:'medium' }}
// 路由配置
angular.module('app', ['ngRoute'])
.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/home.html',
controller: 'HomeController'
})
.when('/users', {
templateUrl: 'views/users.html',
controller: 'UsersController'
})
.otherwise({
redirectTo: '/'
});
});
AngularJS通过其独特的特性和强大的功能,为构建复杂的单页面应用程序提供了完整的解决方案。虽然官方支持已经结束,但其设计理念和实现方式仍然对现代前端开发有着深远的影响。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。