Michael Wang // @loveky on GITHUB
NaN === NaN
false
angular.equals(NaN, NaN)
true
/abc/ === /abc/
false
angular.equals(/abc/, /abc/)
true
obj1 = {a:1, b: function() {alert(1)}, $c: 111}
obj2 = {a:1, b: function() {alert(2)}, $c: 222}
obj1 === obj2
false
angular.equals(obj1, obj2)
true
Use it this way:
function foo(callback) { var result = calculateResult(); (callback || angular.noop)(result); }
function noop() {}
Don't do this:
someModule.controller('MyController', function($scope, dep1, dep2) {
...
$scope.aMethod = function() {
...
}
...
});
Instead, do this:
someModule.controller('MyController', ['$scope', 'dep1', 'dep2', function($scope, dep1, dep2) {
...
$scope.aMethod = function() {
...
}
...
}]);
But why this works?
someModule.controller('MyController', function($scope, dep1, dep2) {
...
$scope.aMethod = function() {
...
}
...
});
When NOT to use controller?
$digest()
Processes all of the watchers of the current scope and
its children.
Usually, you don't call
$digest()
directly, you should use $apply instead.
Except that you may need to call
$digest()
to simulate the scope life cycle in unit test.
$apply()
$apply()
is used to execute an expression in angular from outside of the angular
framework.
For example:
function $apply(expr) {
try {
return $eval(expr);
} catch (e) {
$exceptionHandler(e);
} finally {
$root.$digest();
}
}
$scope.variable = "some value";
executeSomeAction();
$scope.$apply();
$scope.$apply(function() {
$scope.variable = "some value";
executeSomeAction();
});
Angular will broadcast a $destroy event just before tearing down a scope and removing the scope from its parent.
demoApp.controller("HelloController", function($scope, $timeout) {
var ts = new Date();
var onTimeout = function() {
console.log("logger start at: " + ts);
timer = $timeout(onTimeout, 1000);
};
var timer = $timeout(onTimeout, 1000);
});
Don't forget to clean up your $scope!
demoApp.controller("HelloController", function($scope, $timeout) {
var ts = new Date();
var onTimeout = function() {
console.log("logger start at: " + ts);
timer = $timeout(onTimeout, 1000);
};
var timer = $timeout(onTimeout, 1000);
$scope.$on("$destroy", function() {
if (timer) {
$timeout.cancel(timer);
}
});
});
function $LogProvider(){ var debug = true, self = this; this.debugEnabled = function(flag) { if (isDefined(flag)) { debug = flag; return this; } else { return debug; } };
this.$get = ['$window', function($window){ return {
angular.module('app', [])
.config(['$logProvider', function($logProvider){
$logProvider.debugEnabled(false);
}])
Unit Test
End to end test
Protractor(based on WebdriverJS) + Jasmine
Thank you!