angular controller test
14 Feb 2016Testing controller with controllerAs
After we’ve seen how to test a service, let’s talk about testing a controller.
After we’ve seen how to test a service, let’s talk about testing a controller.
suppose you’re using angular and you have a directive that you pass data to:
<my-chart data="chartData"></my-chart>;
suppose the directive needs to perform some logic when data changes. you’ll probably use $watch:
angular.module('app')
.controller('MainController', function($scope){
function dataChange(){
$scope.chartData= {...};
}
})
.directive('myChart', function(){
return {
scope: {data: '='},
link: function(scope){
function redraw(){...}
scope.$watch('data', redraw);
}
};
});
If you try to access a non-existing property of an object you’ll get undefined:
var A = {};
console.log(A.a);//undefined
console.log(A);//Object {}