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

feat($compile): allow using bindToController as object, support both new/isolate scopes #10467

Closed
wants to merge 4 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
12 changes: 12 additions & 0 deletions docs/content/error/$compile/noctrl.ngdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@ngdoc error
@name $compile:noctrl
@fullName Controller is required.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this full name be more like: "controller as" identifier required to bind to controller?

Copy link
Member

@gkalpak gkalpak Jan 19, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I am not mitaken, this error will also be thrown if there is bindToController: true but no controler:.... So the literals have to account for all cases (i.e. bindToController: true but no controller or no controller identifier).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I see that now. I was thrown by the main body of the error document, which doesn't really mention that aspect of the error at all.
I wonder if we ought to have two separate errors?

@description

When using the `bindToController` feature of AngularJS, a directive is required
to have a Controller. A controller may be specified by adding a "controller"
property to the directive definition object. Its value should be either a
string, or an invokable object (a function, or an array whose last element is a
function).

For more information, see the {@link guide/directive directives guide}.
71 changes: 71 additions & 0 deletions docs/content/error/$compile/noident.ngdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
@ngdoc error
@name $compile:noident
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$compile:noalias

@fullName Controller identifier is required.
@description

When using the `bindToController` feature of AngularJS, a directive is required
to have a Controller identifier, which is initialized in scope with the value of
the controller instance. This can be supplied using the "controllerAs" property
of the directive object, or alternatively by adding " as IDENTIFIER" to the controller
name.

For example, the following directives are valid:

```js
// OKAY, because controller is a string with an identifier component.
directive("okay", function() {
return {
bindToController: true,
controller: "myCtrl as $ctrl"
scope: {
text: "@text"
}
};
});


// OKAY, because the directive uses the controllerAs property to override
// the controller identifier.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// the controller alias

directive("okay2", function() {
return {
bindToController: true,
controllerAs: "$ctrl",
controller: function() {

},
scope: {
text: "@text"
}
};
});
```

While the following are invalid:

```js
// BAD, because the controller property is a string with no identifier.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// BAD, because the controller property is a string with no alias.

directive("bad", function() {
return {
bindToController: true,
controller: "noIdentCtrl",
scope: {
text: "@text"
}
};
});


// BAD because the controller is not a string (therefore has no identifier),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// BAD because the controller is not a string (therefore has no alias),

// and there is no controllerAs property.
directive("bad2", function() {
return {
bindToController: true,
controller: function noControllerAs() {

},
scope: {
text: "@text"
}
};
});
```
3 changes: 3 additions & 0 deletions src/.jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@
"urlResolve": false,
"urlIsSameOrigin": false,

/* ng/controller.js */
"identifierForController": false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"aliasForController": false,


/* ng/compile.js */
"directiveNormalize": false,

Expand Down
Loading