Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
fix(compiler): make bindToController vars available to instantiation fn
Browse files Browse the repository at this point in the history
closes #1534
  • Loading branch information
rschmukler committed May 3, 2015
1 parent 7e0a2aa commit e414091
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/core/services/compiler/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function mdCompilerService($q, $http, $injector, $compile, $controller, $templat
* the element and instantiate the provided controller (if given).
* - `locals` - `{object}`: The locals which will be passed into the controller once `link` is
* called. If `bindToController` is true, they will be coppied to the ctrl instead
* - `bindToController` - `bool`: bind the locals to the controller, instead of passing them in. These values will not be available until after initialization.
* - `bindToController` - `bool`: bind the locals to the controller, instead of passing them in.
*/
this.compile = function(options) {
var templateUrl = options.templateUrl;
Expand Down Expand Up @@ -115,10 +115,11 @@ function mdCompilerService($q, $http, $injector, $compile, $controller, $templat

//Instantiate controller if it exists, because we have scope
if (controller) {
var ctrl = $controller(controller, locals);
var invokeCtrl = $controller(controller, locals, true);
if (bindToController) {
angular.extend(ctrl, locals);
angular.extend(invokeCtrl.instance, locals);
}
var ctrl = invokeCtrl();
//See angular-route source for this logic
element.data('$ngControllerController', ctrl);
element.children().data('$ngControllerController', ctrl);
Expand Down
8 changes: 7 additions & 1 deletion src/core/services/compiler/compiler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,22 @@ describe('$mdCompiler service', function() {
}));

it('should work with bindToController', inject(function($rootScope) {
var called = false;
var data = compile({
template: 'hello',
controller: function() { },
controller: function($scope) {
expect(this.name).toBe('Bob');
expect($scope.$apply).toBeTruthy(); // test DI working properly
called = true;
},
controllerAs: 'ctrl',
bindToController: true,
locals: { name: 'Bob' }
});
var scope = $rootScope.$new();
data.link(scope);
expect(scope.ctrl.name).toBe('Bob');
expect(called).toBe(true);
}));
});
});
Expand Down

0 comments on commit e414091

Please sign in to comment.