分类目录归档:前端
javascript – how can we use $compile outside a directive in Angularjs
JavaScript中模块模式的应用
模块模式是JavaScript中使用最广泛的模式,模块利用了闭包的特性
Module模式的基本特征:
- 模块化,可重用
- 封装了变量和function,和全局的namaspace不接触,松耦合
- 只暴露可用public的方法,其它私有方法全部隐藏
//自执行函数(immediately invoked function)
(function(){
}());
//这样也可以(this works too)
(function(){})();
//增加参数(add params to first)
(function(my){
}(variable));
//增加返回值(add return)
(function(my){//
return my;
}(variable));
//这样MODULE就是返回的my(give my to MODULE)
//js加载完就会运行这个函数,并且把返回值赋予MODULE
var MODULE = (function(my){//
return my;
}(variable));
//把自执行函数的参数换成返回的变量(change immediately invoked function parameter)
var MODULE = (function(my){//
return my;
}(MODULE || {}));
//你可以在其他任意地方这样定义MODULE
//所有的方法都会可以用MODULE来调用
//now you can define MODULE anywhere you like,
//all the functions inside will be extended to others
// 增加功能(add capabilities)
var MODULE = (function (my) {
//you cannot access inner variable
var variable = {};
// this is inner function, you cannot call it outside of the MODULE
function innerFun(){
console.log(variable);//you can access inner variable here
}
//this function will be exported, you can call it outside
my.someFun = function(){
//you can call inner variable and function here
console.log(variable);
innerFun();
};
return my;
}(MODULE || {}));
//MODULE || {} is the parameter passed to this MODULE,
//equals "my" inside MODULE
//you can define MODULE anywhere else
AngularJS中使用filter显示数字的千分位号
点击Result查看效果。
Data Types in JavaScript
null and undefined in javascript
> var un;
> console.log(un);
undefined
> var nu = null;
> console.log(nu);
null
> typeof un
'undefined'
> typeof nu
'object'
> un == nu
true
> un === nu
false
> Number(undefined)
NaN
> Number(null)
undefined表示”缺少值”,就是此处应该有一个值,但是还没有定义。典型用法是:
(1)变量被声明了,但没有赋值时,就等于undefined。
(2) 调用函数时,应该提供的参数没有提供,该参数等于undefined。
(3)对象没有赋值的属性,该属性的值为undefined。
(4)函数没有返回值时,默认返回undefined。
使用css制作对话气泡的样式(下方小箭头)css bubble – JSFiddle
在html中使用css截短文字css truncate text – JSFiddle
用css截短文字有一个好处,就是不用考虑全角半角,文字语言等因素,比后台代码(java、.Net、php等)简单方便
举个栗子:
Angular简单的判断情况绑定数据 AngularJS simple conditional binding
在AngularJS的使用$apply更新model
先看一个不能work的
如果ng-app和ng-controller写在一个dom里,这样就不能更新model里的值
分开写就没问题
解决这个问题可以这样:
当然也可以这样:
function Ctrl($scope) { $scope.message = "Waiting 2000ms for update"; setTimeout(function () { $scope.message = "Timeout called!"; $scope.$apply(); }, 2000); }