I'm unit testing a controller and I want to check whether the view is correctly updated. Some field of the view are related to fields/functions of the controller. When I try to get the compiled view, I'm not able to link the controller to the linking function with his constructor. I've created a plunker
function($rootScope, $scope, $compile, $timeout) {
$scope.helloScope = 'Hi Scope';
this.hello = 'Hello';
this.sayFunction = function() {
return 'Function';
};
var template =
"<div>{{helloScope}} {{main.hello}} {{main.sayFunction()}}</div>";
$scope.toCompile = template;
var newScope = $rootScope.$new();
//newScope.main = this; //if I uncomment this line, all works!
newScope.helloScope = 'Hi Scope';
var el = angular.element(template);
var linkFn = $compile(el);
var view = linkFn(newScope,
undefined, {transcludeControllers:{'main':this}});
$timeout(function() {
$scope.compiled = view.html();
});
}
it works only when I add to the newScope a main property, so I suppose I'm not using the linkFn() function properly, but I have not been able to find any documents or examples about it.
[edit] I've found in angular documentation that the third parameter for the linkFn is "transcludeControllers - an object hash with keys that map controller names to controller instances; if given, it will make the controllers available to directives.", but it seems not to work for me