Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Disallow non-isolate scope & default controllerAs to be $ctrl #13710

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
* registered controller} if passed as a string. An empty `noop` function by default.
* - `controllerAs` – `{string=}` – identifier name for to reference the controller in the component's scope.
* If present, the controller will be published to scope under the `controllerAs` name.
* If not present, this will default to be the same as the component name.
* If not present, this will default to be `$ctrl`.
* - `template` – `{string=|function()=}` – html template as a string or a function that
* returns an html template as a string which should be used as the contents of this component.
* Empty string by default.
Expand All @@ -967,7 +967,6 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
* See {@link ng.$compile#-bindtocontroller- `bindToController`}.
* - `transclude` – `{boolean=}` – whether {@link $compile#transclusion content transclusion} is enabled.
* Disabled by default.
* - `isolate` – `{boolean=}` – whether the new scope is isolated. Isolated by default.
* - `restrict` - `{string=}` - a string containing one or more characters from {@link ng.$compile#-restrict- EACM},
* which restricts the component to specific directive declaration style. If omitted, this defaults to 'E'.
* - `$canActivate` – `{function()=}` – TBD.
Expand All @@ -982,21 +981,21 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
* directives. Component definitions usually consist only of a template and a controller backing it.
*
* In order to make the definition easier, components enforce best practices like use of `controllerAs`,
* `bindToController` and default behaviors like **isolate scope** and restriction to elements.
* `bindToController`, **isolate scope** and default behaviors like restriction to elements.
*
* Here are a few examples of how you would usually define components:
*
* ```js
* var myMod = angular.module(...);
* myMod.component('myComp', {
* template: '<div>My name is {{myComp.name}}</div>',
* template: '<div>My name is {{$ctrl.name}}</div>',
* controller: function() {
* this.name = 'shahar';
* }
* });
*
* myMod.component('myComp', {
* template: '<div>My name is {{myComp.name}}</div>',
* template: '<div>My name is {{$ctrl.name}}</div>',
* bindings: {name: '@'}
* });
*
Expand Down Expand Up @@ -1067,11 +1066,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
var template = (!options.template && !options.templateUrl ? '' : options.template);
return {
controller: options.controller || function() {},
controllerAs: identifierForController(options.controller) || options.controllerAs || name,
controllerAs: identifierForController(options.controller) || options.controllerAs || '$ctrl',
template: makeInjectable(template),
templateUrl: makeInjectable(options.templateUrl),
transclude: options.transclude,
scope: options.isolate === false ? true : {},
scope: {},
bindToController: options.bindings || {},
restrict: options.restrict || 'E'
};
Expand Down
5 changes: 2 additions & 3 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9333,7 +9333,7 @@ describe('$compile', function() {
inject(function(myComponentDirective) {
expect(myComponentDirective[0]).toEqual(jasmine.objectContaining({
controller: jasmine.any(Function),
controllerAs: 'myComponent',
controllerAs: '$ctrl',
template: '',
templateUrl: undefined,
transclude: undefined,
Expand All @@ -9352,7 +9352,6 @@ describe('$compile', function() {
template: 'abc',
templateUrl: 'def.html',
transclude: true,
isolate: false,
bindings: {abc: '='},
restrict: 'EA'
});
Expand All @@ -9364,7 +9363,7 @@ describe('$compile', function() {
template: 'abc',
templateUrl: 'def.html',
transclude: true,
scope: true,
scope: {},
bindToController: {abc: '='},
restrict: 'EA'
}));
Expand Down