mercredi 28 octobre 2015

Using alias controller in templates to compile

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 costructor. I'v 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,undefined, {controller:this,controllerAs:'main'});
  $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.

Aucun commentaire:

Enregistrer un commentaire