Angular入门
初识Angular,理解它的基本设计原理可以更好的把握Angular。看了慕课网大漠穷秋老师的视频,总结一下。下面的源码也来自慕课网,可以下载到。 1 Angular的四大特点MVC、模块化、双向数据绑定、指令系统。 2 MVC2.1 什么是MVC,为什么要用MVC?MVC:模型、视图、控制器,互联网的三层架构:视图负责客户端与用户交互,控制器负责中间业务逻辑的处理,互通了视图和数据,模型用语对数据的增删改查。 2.2 前端实现MVC有哪些困难实现MVC思路是很清晰的,控制器就是大脑,负责把模型中的数据交给视图来展现,交互过程中又通过大脑来操作数据,改变视图。
2.3 Angular如何实现MVC2.3.1 在Angular中,M、V、C分别是什么?<!doctype html> <html ng-app> <head> <meta charset="utf-8"> </head> <body> <div ng-controller="HelloAngular"> <p>{{greeting.text}},Angular</p> </div> </body> <script src="js/angular-1.3.0.js"></script> <script src="HelloAngular_MVC.js"></script> </html> js/angular-1.3.0.js 是Angular的源文件 function HelloAngular($scope) { $scope.greeting = { text: 'Hello' }; } 视图很好理解, 2.3.2 Controller的使用误区1.不要复用controller,需要复用的地方又service实现,不良写法: <div ng-controller="CommonController"> <div ng-controller="Controller1"> <p>{{greeting.text}},Angular</p> <button ng-click="test1()">test1</button> </div> <div ng-controller="Controller2"> <p>{{greeting.text}},Angular</p> <button ng-click="test2()">test2</button> <button ng-click="commonFn()">通用</button> </div> </div> Controller: function CommonController($scope){ $scope.commonFn=function(){ alert("这里是通用功能!"); }; } function Controller1($scope) { $scope.greeting = { text: 'Hello1' }; $scope.test1=function(){ alert("test1"); }; } function Controller2($scope) { $scope.greeting = { text: 'Hello2' }; $scope.test2=function(){ alert("test2"); } } 2.不要用controller操作DOM,因为controller操作DOM的代价很昂贵,需要重绘或者重新布局,angular中通过指令操作DOM。 2.3.3 如何复用Model跑一下下面的代码吧: <!doctype html> <html ng-app> <head> <meta charset="utf-8"> </head> <body> <div> <input ng-model="greeting.text"/> <p>{{greeting.text}},Angular</p> </div> </body> <script src="js/angular-1.3.0.js"></script> </html>
p元素内容会随文本框内容实时动态变更。 2.3.4 如何复用Viewangular可以自己创建html标签,可以自己制作标签了,好开心,制作一次使用n次,便可以实现view的复用: <!doctype html> <html ng-app="MyModule"> <head> <meta charset="utf-8"> </head> <body> <hello></hello> </body> <script src="js/angular-1.3.0.js"></script> <script src="HelloAngular_Directive.js"></script> </html> HelloAngular_Directive.js: var myModule = angular.module("MyModule",[]); myModule.directive("hello",function() { return { restrict: 'E',template: '<div>Hi everyone!</div>',replace: true } }); 通过指令便可满足!指令在指令一节会细讲。 2.3.5 angular中的作用域$scope在控制器和模型中我们都使用了作用域,$rootscope是根作用域,由 <!doctype html> <html ng-app> <head> <meta charset="utf-8"> <link rel="stylesheet" type="text/css" href="Scope1.css" /> </head> <body> <div ng-controller="EventController"> Root scope <tt>MyEvent</tt> count: {{count}} <ul> <li ng-repeat="i in [1]" ng-controller="EventController"> <button ng-click="$emit('MyEvent')"> $emit('MyEvent') </button> <button ng-click="$broadcast('MyEvent')"> $broadcast('MyEvent') </button> <br> Middle scope <tt>MyEvent</tt> count: {{count}} <ul> <li ng-repeat="item in [1,2,3]" ng-controller="EventController"> Leaf scope <tt>MyEvent</tt> count: {{count}} </li> </ul> </li> </ul> </div> </body> <script src="js/angular-1.3.0.js"></script> <script src="Scope2.js"></script> </html> Scope2.js: function EventController($scope) { $scope.count = 0; $scope.$on('MyEvent',function() { $scope.count++; }); } $emit('MyEvent')可以控制控制器中事件控制本级和上级模型, 3 模块化3.1 没有模块化的项目组织思考一下,我们现在会如何开发一个项目呢?一个项目会有多个文件,这些文件又该如何组织呢,一种方法是:
这里需要借助模块化来解决问题。 3.2 有模块化的项目组织3.2.1 何为模块,模块就是实现一定功能的程序集合,这个集合中包括控制、视图、服务、过滤等。 3.2.2 angular中的模块实现<!doctype html> <html ng-app="HelloAngular"> <head> <meta charset="utf-8"> </head> <body> <div ng-controller="helloNgCtrl"> <p>{{greeting.text}},Angular</p> </div> </body> <script src="js/angular-1.3.0.js"></script> <script src="NgModule1.js"></script> </html> NgModule1.js: var helloModule=angular.module('HelloAngular',[]); helloModule.controller('helloNgCtrl',['$scope',function($scope){ $scope.greeting = { text: 'Hello' }; }]);
3.2.3 模块化优势这样,便解决了1.变量污染全局的问题,并且可以2.按照功能划分模块。 3.3.4 模块化实现首先先了解一个项目的目录,再具体讲解如何实现依赖注入。以BookStore 为例
<!doctype html> <html ng-app="bookStoreApp"> <head> <meta charset="UTF-8"> <title>BookStore</title> <script src="framework/1.3.0.14/angular.js"></script> <script src="framework/1.3.0.14/angular-route.js"></script> <script src="framework/1.3.0.14/angular-animate.js"></script> <script src="js/app.js"></script> <script src="js/controllers.js"></script> <script src="js/filters.js"></script> <script src="js/services.js"></script> <script src="js/directives.js"></script> </head> <body> <div ng-view> </div> </body> </html> 主要是对一些资源文件的加载,这里通过路由控制加载视图 app.js: var bookStoreApp = angular.module('bookStoreApp',[ 'ngRoute','ngAnimate','bookStoreCtrls','bookStoreFilters','bookStoreServices','bookStoreDirectives' ]); bookStoreApp.config(function($routeProvider) { $routeProvider.when('/hello',{ templateUrl: 'tpls/hello.html',controller: 'HelloCtrl' }).when('/list',{ templateUrl:'tpls/bookList.html',controller:'BookListCtrl' }).otherwise({ redirectTo: '/hello' }) }); bookStoreApp后面[]中指明了bookStoreApp依赖的模块,在加载时保证了依赖模块加载完之后才运行本模块,下面的配置项说明了哪个url链接应该对应哪个控制器管理的哪个视图,主要when方法和otherwise方法,otherwise方法说明未指明路由是默认定向到/hello路由。 看一下这个控制器是怎样的 controllers.js: var bookStoreCtrls = angular.module('bookStoreCtrls',[]); bookStoreCtrls.controller('HelloCtrl',function($scope) { $scope.greeting = { text: 'Hello' }; } ]); bookStoreCtrls.controller('BookListCtrl',function($scope) { $scope.books =[ {title:"《Ext江湖》",author:"大漠穷秋"},{title:"《ActionScript游戏设计基础(第二版)》",{title:"《用AngularJS开发下一代WEB应用》",author:"大漠穷秋"} ] } ]); 其它的文件只是给了框架,为了给出一个项目的架构。 var bookStoreDirectives = angular.module('bookStoreDirectives',[]); bookStoreDirectives.directive('bookStoreDirective_1',function($scope) {} ]); bookStoreDirectives.directive('bookStoreDirective_2',function($scope) {} ]); filters.js: var bookStoreFilters = angular.module('bookStoreFilters',[]); bookStoreFilters.filter('bookStoreFilter_1',function($scope) {} ]); bookStoreFilters.filter('bookStoreFilter_2',function($scope) {} ]); services.js: var bookStoreServices = angular.module('bookStoreServices',[]); bookStoreServices.service('bookStoreService_1',function($scope) {} ]); bookStoreServices.service('bookStoreService_2',function($scope) {} ]); 至此,应该了解了一个简单项目的架构和依赖管理。 4 双向数据绑定4.1 什么是双向数据绑定终于讲到双向数据绑定了,这个神奇的东西到底是什么? 4.2 双向数据绑定如何实现4.2.1 从$scope -> view的单向绑定MVC通常的思路是,将模型的数据展示在视图上,那么。如何实现从模型到视图的绑定呢?我们似乎实现过从模型到视图的绑定。 <!doctype html> <html ng-app> <head> <meta charset="utf-8"> </head> <body> <div ng-controller="HelloAngular"> <p><span {{greeting.text}}></span>,Angular</p> </div> </body> <script src="js/angular-1.3.0.js"></script> <script src="HelloAngular_MVC.js"></script> </html> HelloAngular_MVC.js: function HelloAngular($scope) { $scope.greeting = { text: 'Hello' }; }
如果使用 模型到视图不仅可以控制内容,还可以控制样式: <!doctype html> <html ng-app="MyCSSModule"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="NgClass.css"> </head> <body> <div ng-controller='HeaderController'> <div ng-class='{error: isError,warning: isWarning}'>{{messageText}}</div> <button ng-click='showError()'>Simulate Error</button> <button ng-click='showWarning()'>Simulate Warning</button> </div> </body> <script src="js/angular-1.3.0.js"></script> <script src="NgClass.js"></script> </html> NgClass.css: .error { background-color: red; } .warning { background-color: yellow; } NgClass.js: var myCSSModule = angular.module('MyCSSModule',[]); myCSSModule.controller('HeaderController',function($scope) { $scope.isError = false; $scope.isWarning = false; $scope.showError = function() { $scope.messageText = 'This is an error!'; $scope.isError = true; $scope.isWarning = false; }; $scope.showWarning = function() { $scope.messageText = 'Just a warning. Please carry on.'; $scope.isWarning = true; $scope.isError = false; }; } ])
4.2.2 从$scope <-> view的双向绑定给一个实例,来实现双向数据绑定 <!doctype html> <html ng-app="UserInfoModule"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="css/bootstrap-3.0.0/css/bootstrap.css"> <script src="js/angular-1.3.0.js"></script> <script src="Form.js"></script> </head> <body> <div class="panel panel-primary"> <div class="panel-heading"> <div class="panel-title">双向数据绑定</div> </div> <div class="panel-body"> <div class="row"> <div class="col-md-12"> <form class="form-horizontal" role="form" ng-controller="UserInfoCtrl"> <div class="form-group"> <label class="col-md-2 control-label"> 邮箱: </label> <div class="col-md-10"> <input type="email" class="form-control" placeholder="推荐使用126邮箱" ng-model="userInfo.email"> </div> </div> <div class="form-group"> <label class="col-md-2 control-label"> 密码: </label> <div class="col-md-10"> <input type="password" class="form-control" placeholder="只能是数字、字母、下划线" ng-model="userInfo.password"> </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <div class="checkbox"> <label> <input type="checkbox" ng-model="userInfo.autoLogin">自动登录 </label> </div> </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <button class="btn btn-default" ng-click="getFormData()">获取Form表单的值</button> <button class="btn btn-default" ng-click="setFormData()">设置Form表单的值</button> <button class="btn btn-default" ng-click="resetForm()">重置表单</button> </div> </div> </form> </div> </div> </div> </div> </body> </html> Form.js: var userInfoModule = angular.module('UserInfoModule',[]); userInfoModule.controller('UserInfoCtrl',function($scope) { $scope.userInfo = { email: "253445528@qq.com",password: "253445528",autoLogin: true }; $scope.getFormData = function() { console.log($scope.userInfo); }; $scope.setFormData = function() { $scope.userInfo = { email: 'damoqiongqiu@126.com',password: 'damoqiongqiu',autoLogin: false } }; $scope.resetForm = function() { $scope.userInfo = { email: "253445528@qq.com",autoLogin: true }; } } ]) 4.2.3 扩展路由之前讲过用app.js实现页面的控制,但是如果一个页面内部需要局部更新,切换路由,再用 5 指令5.1 指令配置项以 <!doctype html> <html ng-app="MyModule"> <head> <meta charset="utf-8"> </head> <body> <hello></hello> <div hello></div> <div class="hello"></div> <!-- directive:hello --> <div></div> </body> <script src="framework/angular-1.3.0.14/angular.js"></script> <script src="HelloAngular_Directive.js"></script> </html> HelloAngular_Directive.js: var myModule = angular.module("MyModule",function() { return { restrict: 'AEMC',replace: true } });
5.1.1 匹配模式restrict说明了匹配模式:
<hello></hello> <div hello></div> <div class="hello"></div> <!-- directive:hello --> <div></div> 四个 5.1.2 模版template模版指定了指令的内容。 var myModule = angular.module("MyModule",[]); //注射器加载完所有模块时,此方法执行一次 myModule.run(function($templateCache){ $templateCache.put("hello.html","<div>Hello everyone!!!!!!</div>"); }); myModule.directive("hello",function($templateCache) { return { restrict: 'AECM',template: $templateCache.get("hello.html"),replace: true } }); 5.1.3 替换当 <hello> <div>这里是指令内部的内容。</div> </hello>
var myModule = angular.module("MyModule",function() { return { restrict:"AE",transclude:true,template:"<div>Hello everyone!<div ng-transclude></div></div>" } }); 当指令想要嵌套指令时,这种方式可以不让被嵌套的指令被替代掉。以上大概讲解了一下指令,还有很多指令的内容,在基础入门这里就先介绍这么多。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |