2

I'm struggling with this for hours now.

var testApp = angular.module('testApp', []);

testApp.directive('test', function() {
    return {
        restrict: 'E',
        transclude: true,
        template: '<div ng-transclude>Hello World</div>',
        link: function(scope) {

        }
    }
});

testApp.controller('testCtrl', function ($scope) {
    $scope.user = "";
});

Here's JSFiddle: http://jsfiddle.net/2bKPj/

Now, all I need is for an input embedded in directive to be able to reflect user model directly in testCtrl controller. I'm confused on how this beast works since I taught that scopes are shared in this case, no?

1 Answer 1

2

ngTransclude creates a new child scope which protorypically inherits from it's parent scope.

When you use a primitive on the child scope it shadows the parent scope's variable.

It's been already said thousand times: use the dot notation!

controller:

testApp.controller('testCtrl', function ($scope) {
    $scope.data = { user : "Hello World" };
});

html:

<input type="text" ng-model="data.user"/><br />
Directive model:<span>{{ data.user }}</span>  

Check my other answers for description:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.