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

Behaviour bindToController in child scope versus isolated scope #13021

Closed
@jwgmeligmeyling

Description

@jwgmeligmeyling

I was playing around with the bindToController option for directives. I stumbled upon a seemingly strange difference between the behaviour using a child scope compared to an isolated scope. When I use an isolated scope, an new scope is created for the directive, but changes to the bound controller attributes are forwarded to the parent scope. Yet when I use a child scope instead, my example breaks. (Using bindToController using child scopes should be allowed according to http://blog.thoughtram.io/angularjs/2015/01/02/exploring-angular-1.3-bindToController.html#improvements-in-14 )

The code:

    {
        restrict: 'E',
        scope: {},
        controller: 'FooDirCtrl',
        controllerAs: 'vm',
        bindToController: {
            name: '='
        },
        template: '<div><input ng-model="vm.name"></div>'
    };

Working demo https://jsfiddle.net/tthtznn2/

The version using a child scope:

    {
        restrict: 'E',
        scope: true,
        controller: 'FooDirCtrl',
        controllerAs: 'vm',
        bindToController: {
            name: '='
        },
        template: '<div><input ng-model="vm.name"></div>'
    };

Demo: http://jsfiddle.net/ydLd1e00/

The changes to name are forwarded to the child scope, but not to the parent scope. This in contrast to binding to an isolated scope, where the changes are forwarded to the old scope. Why is this?

*As copied from http://stackoverflow.com/questions/32972354/behaviour-bindtocontroller-in-child-scope-versus-isolated-scope *

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions