版本:css
获取方式html
不能写if else语句前端
<div>{{val}}</div> <div>{{'angularjs'+'很好很强大'}}</div> <div>{{1+1}}</div> <div>{{1==1?'相等':'不相等'}}</div> <div>{{getItem()}}</div>
在使用了angularjs的页面,以ng-开头的属性,称之为指令vue
<body ng-app></body> <input type="text" ng-model="val" ng-init="val">
ng-appnode
ng-model="变量"react
ng-click=" "angularjs
ng-initweb
模块化四步ajax
建立模块,myApp是模块的名字,module方法,数组中要依赖的模块的名字,用逗号链接。没有要依赖的文件的话,传空数组。npm
<script> var app=angular.module('myApp',[]); </script>
ng-app后面写模块的名字,告诉angularjs当前的页面由本身建立的myApp模块去管理
<body ng-app="myApp"></body>
建立控制器,建立模块的语句的返回值就是一个模块对象,用这个对象去点方法,就是建立控制器
<script> app.controller('demoCtrlA',function(){ alert(1) }); app.controller('demoCtrlB',function(){ alert(2) }); </script>
告诉angularjs当前区域由这个控制器去控制
<div class="sideBar" ng-controller="demoCtrlA"></div> <div class="main" ng-controller="demoCtrlB"></div>
在控制器中封装两个函数
<body ng-app="myApp" ng-controller="demoCtrl"> <input type="text" ng-model="val"> <div>{{val}}</div> <button ng-click="setValue()">设置值</button> <button ng-click="getValue()">获取值</button> <script src="node_modules/angular/angular.min.js"></script> <script> var app=angular.module('myApp',[]); app.controller('demoCtrl',function($scope){ $scope.val="我是初始值"; $scope.setValue=function(){ $scope.val="我是经过setValue方法更改的值" } $scope.getValue=function(){ alert($scope.val) } }) </script> </body>
传参
传递多个参数,直接接在后面写,只要和后面一一对应起来就能够
<ul> <li><a href="#!/article/1/我是第1篇文章">我是第1篇文章</a></li> <li><a href="#!/article/2/我是第2篇文章">我是第2篇文章</a></li> <li><a href="#!/article/3/我是第3篇文章">我是第3篇文章</a></li> </ul> <script> var app=angular.module('myApp',['ngRoute']) app.config(function($routeProvider){ $routeProvider .when('/article/:id/:title',{ templateUrl:'articleTpl', controller:'articleCtrl' }) }); </script>
单页web应用的应用场景:单页面应用对搜索引擎不友好,不适合作面向大众的展现型网站,网站后台管理系统、办公OA、混合App等等不须要被搜索引擎搜索到的应用
<script src="node_modules/angular/angular.min.js"></script> <script src="node_modules/angular-route/angular-route.min.js"></script> <body ng-app="myApp"> <a href="#!/index">首页</a> <a href="#!/list">列表页</a> <div ng-view></div> </body> <script> var app=angular.module('myApp',['ngRoute']) app.config(function($routeProvider){ $routeProvider .when('/index',{ templateUrl:'./tpl/index.html', controller:'indexCtrl' }) .when('/list',{ templateUrl:'./tpl/list.html', controller:'listCtrl' }) .otherwise('/index') }); app.controller('indexCtrl',function($scope){ $scope.msg="我是首页msg" }) app.controller('listCtrl',function($scope){ $scope.msg="我是列表页msg" }) </script>
<script> templateUrl:'./tpl/index.html'//localhost template:'<div>我是首页</div>'//file|localhosst template:'indexTpl'//file|localhosst </script>
压缩的时候,不会对字符串进行压缩,因此数组中传递字符串来肯定参数的顺序
<script> angular.module("myApp",[]).controller("demoCtrl",["$scope","$timeout","$http","$location",function(a,b,c,d){ a.msg="我是msg" }]) </script>
ng-repeat="循环过程当中的当前项 in 数据"循环数据并生成当前DOM元素
<ul> <li ng-repeat="item in arr">{{item}}</li> </ul>
遍历数组对象,能够嵌套,有ng-repeat的标签中还能够嵌套ng-repeat的标签
<ul> <li ng-repeat="person in person"> {{person.name}}{{person.age}} <span ng-repeat="item in person.hobby">{{item}}</span> </li> </ul>
数组项重复,会报错
<ul> <li ng-repeat="item in arr track by $index">{{item}}</li> </ul>
事件指令
res是形参,表示请求回来的数据
<script src="node_modules/angular/angular.js"></script> <script src="node_modules/angular-sanitize.min.js"></script> <script> angular.module('myApp',['ngSanitize']) .controller('demoCtrl',['$scope','$http',function($scope,$http){ $http({ url:'./test.json', method:'post',//请求方式 params:{//get传参 a:1, b:2 }, data:{ c:3, d:4 } }).then(function(res){ //成功回调 console.log(res) },function(){ //失败回调 }) //另一种写法 $http.get('./test.json',{params:{a:1,b:2}}).then(function(res){ //get方式传参 console.log(res) }) $http.post('./test.json',{c:3,d:4}.then(function(res){ //post方式传参 console.log(res) }) }]) </script>
页面一上来的时候,回调函数会先执行一次
<script> $scope.$watch('val',function(newValue,oldValue){ if(newValue.length>10){ $scope.tips="大于10"; }else{ $scope.tips="小于10" } }) </script>
若是自定义指令中的内容是用标签包裹的,会被解析出来
<script> angular.module('myApp',[]) .directive('myHeader',[function(){ return { templateUrl:'myHeaderTpl', replace:true, transclude:true } }]) </script>
link中的函数有三个参数
return中有scope,默认是false,这时,link中的scope就是控制器中的$scope,若是将scope设置成turn,指令就有了单独的做用域
<script> angular.module('myApp',[]) .directive('myDir',[function(){ return { link:function(scope,element,attributes){ element.css('background','red') element.css('background',attributes.myDir) } } }]) </script>
分类
MVC后端思想
MVVN
过滤器
内置过滤器
自定义过滤器
<script> angular.module('myApp',[]) .filter('phonestar',[function(){ return function(value){ return value.substr(0,3)+'****'+value.substr(7) } }]) </script>
ui-router
name是路由名字,必须存在
<script> angular.module('myApp',['ui.router']) .config(['$stateProvider','$urlRouterProvider',function($stateProvider,$urlRouterProvider){ // $stateProvider 配置路由的对象 $stateProvider .state({ url:'/index', // 锚点值 templateUrl:'indexTpl', name:'index', controller:'indexCtrl' }) .state({ url:'/list', // 锚点值 templateUrl:'listTpl', name:'list', controller:'listCtrl' }) // 当没有匹配到的路由时 默认跳转到首页 $urlRouterProvider.otherwise('/index'); }]) .controller('indexCtrl',['$scope',function($scope){ $scope.msg = "indexCtrl"; }]) .controller('listCtrl',['$scope',function($scope){ $scope.msg = "listCtrl"; }]) </script>
概念:
说明
下载gulp
使用gulp编写任务
就像使用angularjs框架须要引包同样,要使用gulp也须要引包
// require('包名') 引包 var gulp = require('gulp'); // gulp变量是对象类型 // 咱们编写任务 处理任务须要用到gulp对象下面的方法
编写人生中的第一个gulp任务
// gulp.task('任务名称',任务回调函数) 建立任务方法 // 任务名称的用处:在执行任务的时候须要指定任务名称 // 回调函数:要作的事情须要写在回调函数中 好比less解析 代码压缩... gulp.task('first',function(){ // gulp.src('文件路径') 获取文件 // 获取任务要处理的文件 gulp.src('./css/base.css') // pipe('怎样处理') 处理任务 // gulp.dest('文件路径') 将处理好的文件放入参数路径中 .pipe(gulp.dest('dist/css')) });
执行任务
gulp中提供的方法