Skip to content

Commit 0824b56

Browse files
committed
added bindToController style
1 parent 5585ed3 commit 0824b56

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,53 @@ While this guide explains the *what*, *why* and *how*, I find it helpful to see
12421242
<div>min={{vm.min}}<input ng-model="vm.min"/></div>
12431243
```
12441244
1245+
###### [Style [Y076](#style-y076)]
1246+
1247+
- Use `bindToController = true` when using `controller as` syntax with a directive when you want to bind the outer scope to the directive's controller's scope.
1248+
1249+
*Why?*: It makes it easy to bind outer scope to the directive's controller scope.
1250+
1251+
Note: `bindToController` was introduced in Angular 1.3.0.
1252+
1253+
```html
1254+
<div my-example max="77"></div>
1255+
```
1256+
1257+
```javascript
1258+
angular
1259+
.module('app')
1260+
.directive('myExample', myExample);
1261+
1262+
function myExample() {
1263+
var directive = {
1264+
restrict: 'EA',
1265+
templateUrl: 'app/feature/example.directive.html',
1266+
scope: {
1267+
max: '='
1268+
},
1269+
controller: ExampleController,
1270+
controllerAs: 'vm',
1271+
bindToController: true
1272+
};
1273+
1274+
return directive;
1275+
}
1276+
1277+
function ExampleController() {
1278+
var vm = this;
1279+
vm.min = 3;
1280+
console.log('CTRL: vm.min = %s', vm.min);
1281+
console.log('CTRL: vm.max = %s', vm.max);
1282+
}
1283+
```
1284+
1285+
```html
1286+
<!-- example.directive.html -->
1287+
<div>hello world</div>
1288+
<div>max={{vm.max}}<input ng-model="vm.max"/></div>
1289+
<div>min={{vm.min}}<input ng-model="vm.min"/></div>
1290+
```
1291+
12451292
**[Back to top](#table-of-contents)**
12461293
12471294
## Resolving Promises for a Controller

0 commit comments

Comments
 (0)