0

I have the example below:

HTML:

<body ng-controller="MainCtrl">
    <p>Hello {{name}}!</p>
    <p>Result is {{result}}!</p>
    <output-content data="name"></output-content>
</body>

JavaScript:

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

app.controller('MainCtrl', function($scope) {
    $scope.name = 'World';
    $scope.result = "no";

    $scope.changeLabel = function() {
        $scope.result = 'yes';
    }
});

app.directive('outputContent', function() {
    return {
        restrict: 'E',
        replace: true,
        templateUrl: 'outputContent.html',
        scope: {
            data: '=',
            result: '='
        },
        controller: 'MainCtrl'
    };

});

outpuContent.html:

<div>
    {{data}}
    <button ng-click="changeLabel()">Change</button>
</div>

Plunker is: http://plnkr.co/edit/uzSrGcyQLeRNEIH7IXBf

I would like the result to be 'yes' when I click on the 'Change' button.

It doesn't work.

Could you please explain to me how to write the directive to do so ?

Regards.

2
  • I'm not sure about the complete directive but you output {{data}} in outputContent.html while the changeLabel() function updates $scope.result. Commented May 25, 2015 at 12:17
  • Yes I want to change the result. Data is not used. Commented May 25, 2015 at 12:20

3 Answers 3

3

You define variable in directive scope, but do not pass it. Just pass "result" into directive. E.g:

<output-content data="name" result="result"></output-content>

I forked your plunkr: http://plnkr.co/edit/12FXjUsVdPXhPIQ1mlHt?p=preview

Hope it helpful.

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

1 Comment

I was pleased to help you.
2

In directive, delete scope . Now like This.. this will be work..

app.directive('outputContent', function() {
  return {
    restrict: 'E',
    replace: true,
    templateUrl: 'outputContent.html',
    controller: 'MainCtrl'
  };

});

Comments

1

Scope is not needed. Kindly delete the scope.

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

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
  $scope.result = "no";

  $scope.changeLabel = function() {
    $scope.result = 'oh yeah';
  }
});

app.directive('outputContent', function() {
  return {
    restrict: 'E',
    replace: true,
    templateUrl: 'outputContent.html',
      controller: 'MainCtrl'
  };

});

1 Comment

But I need the isolated scope for 'data'.

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.