最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular Directives: scope vs bindToController - Stack Overflow

programmeradmin11浏览0评论

Since Angular v1.4, it's possible to do this:

scope: {},
bindToController: {
    name: "="
}

instead of old way of doing:

scope: {
    name: "="
},
bindToController: true

Except being more intuitive, is there any difference between them?

Since Angular v1.4, it's possible to do this:

scope: {},
bindToController: {
    name: "="
}

instead of old way of doing:

scope: {
    name: "="
},
bindToController: true

Except being more intuitive, is there any difference between them?

Share Improve this question edited Jan 24, 2016 at 15:29 Ahmet Cetin asked Jan 24, 2016 at 15:22 Ahmet CetinAhmet Cetin 3,8234 gold badges27 silver badges35 bronze badges 3
  • 2 You could, in theory, pass many parameters to the directive, and only bind a few of them to the controller. I can't think of any valid reason to do this, but it is possible. With the original syntax, it was all or nothing. – Claies Commented Jan 24, 2016 at 15:26
  • 1 blog.thoughtram.io/angularjs/2015/01/02/… checkout this insightful article for a bit of explanation – Yerken Commented Jan 24, 2016 at 15:29
  • Possible duplicate of bindToController: Object in directives – Estus Flask Commented Jan 24, 2016 at 21:07
Add a comment  | 

1 Answer 1

Reset to default 13

Think about bindToController as a migration path for future version of Angular.

We prefer to write directives (or components) with isolated scope and bind to controller the properties you want to pass.

Binding variables from scope will gradually disappear.

In the new release of angular (1.5) you don't need to use scope or bindToController, because the scope is isolated for default and for bind variables to controller you can use bindings.

This is also useful for preventing $scope use. Read this article if you want more info about that: https://toddmotto.com/no-scope-soup-bind-to-controller-angularjs/

发布评论

评论列表(0)

  1. 暂无评论