AngularJS系列——对象详解
来源:互联网 发布:2017淘宝活动大全 编辑:程序博客网 时间:2024/06/07 07:28
1. angular对象
* 由angular.js提供的全局变量
* 方法:
* module(name, []) : 创建模型对象
* bootstrap(document, [name]) : 编码启动angular, 代替ng-app
* element(dom元素对象) : 将dom对象/html标签包装为jQuery对象
* forEach() : 遍历数组和元素集合伪数组
* toJson()和fromJson() : js对象与Json字符串相互转换
* isArray(),isObject(),isFunction() :类型判断
* lowercase()和uppercase() : 大小写转换
//手动启动angular应用 angular.bootstrap(document, ['myApp'])
2. module对象
* 由angular.module()创建
* 方法
* controller() : 定义控制器
* factory() : 定义服务对象
* service() : 定义服务对象
* filter() : 定义过滤器
* directive() : 定义指令
* config() : 指定做一些配置的回调函数
angular.module('myApp',[]) .factory('fService', function () { console.log('factory()'); return function () { console.log('fService()...'); } }) .service('sService', function () { console.log('service()'); this.test = function () { console.log('sService test()'); } }) .filter('leftTrim', function () { return function (str) { return str.replace(/^\s+/, ''); }//检查字符串前边是够有空格,有的话去掉 }) .config(function () { console.log('config()'); //配置路由(后面讲) }) .controller('MyCtrl', function ($scope, fService, sService) { console.log('MyCtrl()'); fService();//factory创造的就是函数,传过来的也是函数 sService.test();//构造函数传过来的是对象 $scope.msg = ' atguigu '; });
3. $scope对象
* $apply(): 强制脏数据检查
* 当scope中的数据发生了改变, angular会将数据同步显示到页面, 这一操作称为"脏数据检查"
* angular在它的方法执行完后, 都会进行脏数据检查,
* 在原生JS函数中改变作用域数据, angular是不会进行脏数据检查的
* 使用$scope的$apply函数可以解决此问题
$scope.$apply(function(){
//在这里更新scope数据, angular会同步更新其页面数据
});
* $apply()的本质是调用$digest()去进行脏数据检查更新页面的
$scope.$digest()
* $digest()虽然也能实现页面更新, 但建议使用$apply()
* $watch(): 监视scope中某个属性的变化
* angular是双向数据绑定的, Scope中的属性数据发生了改变, 会自动更新页面中对应的数据
* 但有时, 我们想监视数据的变化情况, 来实现特定的功能
* Angular提供了$watch()来实现
* var unWatch =$scope.$watch('propertyName', function(newValue, oldValue){}, deepWatch)
* 参数一: 指定监视属性的属性名
* 参数二: 发现数据发生改变时的回调函数
* 参数三: 是否深度监视, 默认是false, 代表只是监视属性本身, 而不监视其内部数据
* 返回值: 用于取消监视的函数, 调用unWatch()取消监视
var unWatch = $scope.$watch('msg',function (newMsg,oldMsg) { if(newMsg!==oldMsg){ $scope.count++; if($scope.count === 10){ //取消监视 unWatch(); $scope.count = '已达到最大值(10)' } } });
- AngularJS系列——对象详解
- AngularJS系列——简介
- AngularJS系列——入门
- AngularJS—AngularJS 工作原理详解
- AngularJS系列——目录结构
- AngularJS系列——ui-router
- AngularJS系列(一)——常用指令
- AngularJS系列(三)——过滤器(filder)
- AngularJS系列(四)——服务
- AngularJS系列(五)——下拉框
- AngularJS系列(六)——表格
- AngularJS系列(八)——事件
- AngularJS系列(九)——模块
- AngularJS系列之JavaScript函数和对象
- 深究AngularJS——ui-router详解
- 深究AngularJS——ui-router详解
- AngularJS——ui-router详解
- AngularJS系列——双向绑定和依赖注入
- 深度学习 caffe windows cpu matlab安装
- spacemacs 用semantic重构变量名
- 数位dp
- 关于 MySQL 你可能不知道的 SQL 使用技巧
- MFC学习(01) 矩形框选择移动三角形 (VS2015版本)
- AngularJS系列——对象详解
- Java新特性之可变参数
- 解决mysql-5.7.18安装,MySQL服务无法启动
- 集合之Map
- 基于Raspbian(树莓派)搭建web安全练习环境(一)
- Sublime Text 3 快捷键汇总
- Log4J的基本配置
- Unable to retrieve metadata for procedure
- 适合做程序员的人:像机器一样思考