Angular How to access controller scope from directives

Mou

i am learning angular. so stuck to access controller scope from directive. $scope.vojta has been populated in controller which i like to print it from directive.

<div ng-controller="Controller">
  <my-customer ></my-customer>
</div>

angular.module('docsIsolationExample', [])
.controller('Controller', ['$scope', function($scope) {
  //$scope.naomi = { name: 'Naomi', address: '1600 Amphitheatre' };
  $scope.vojta = { name: 'Vojta', address: '3456 Somewhere Else' };
}])
.directive('myCustomer', function() {
  return {
    restrict: 'E',
    /*
    scope: {
      customerInfo: '=info'
    },
    */
    template: 'Name: {{vojta.name}} Address: {{vojta.address}}'

  };
});

no name or address is printing. not being able to understand where i made the mistake. looking for some suggestion. here is fiddle https://jsfiddle.net/tridip/3g9yddf5/

Pankaj Parkar

As you have customerInfo: '=info' inside isolated scope of directive, you should pass value from info attribute(which is use as attribute alias), so that it can available with customerInfo scope variable inside a directive.

<my-customer info="vojta"></my-customer>

And then change directive template to use customerInfo which has passed customer info.

template: 'Name: {{customerInfo.name}} Address: {{customerInfo.address}}'

Demo Fiddle

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to access child directives scope from parent controller

From Dev

AngularJS : How do you access a directives scope from the same directives controller?

From Dev

AngularJS : How do you access a directives scope from the same directives controller?

From Dev

How do you propagate scope values from directives to the controller?

From Dev

Angular JS access scope from outside of controller

From Dev

Angular 2 - How to access directives from Component

From Dev

How to access angular $scope controller in directive link?

From Dev

How to access Child Controller Scope in Parent Controller in Angular?

From Dev

How to inject a variable from scope into an Angular controller?

From Dev

bind $scope in Directives/Controller

From Dev

bind $scope in Directives/Controller

From Dev

Not able to access $scope variable from within a controller function in Angular JS

From Dev

Angular:How to pass scope data to directives

From Dev

Access controller scope from directive

From Dev

Access the scope value from the controller

From Dev

How to access to the controller scope from a directive inside ng-repeat?

From Dev

How to access a scope from a directive in-another controller

From Dev

How do I access a directive scope from a parent directive/controller?

From Dev

Scope inheritance for angular directives

From Dev

angular scope in controllers + directives

From Dev

Scope inheritance for angular directives

From Dev

How to access $viewValue from controller in Angular

From Dev

How to call controller function from directives

From Dev

How to call angular controller scope function from directive with parameters?

From Dev

How to call angular controller scope function from directive with parameters?

From Dev

Angular how to change controller scope from onclick directive

From Dev

How to pass a function to transcluded scope from a controller (angular 1.3)?

From Dev

Angular: how to connect scope in the controller and scope in services?

From Dev

Angular Directives with External Controller

Related Related

  1. 1

    How to access child directives scope from parent controller

  2. 2

    AngularJS : How do you access a directives scope from the same directives controller?

  3. 3

    AngularJS : How do you access a directives scope from the same directives controller?

  4. 4

    How do you propagate scope values from directives to the controller?

  5. 5

    Angular JS access scope from outside of controller

  6. 6

    Angular 2 - How to access directives from Component

  7. 7

    How to access angular $scope controller in directive link?

  8. 8

    How to access Child Controller Scope in Parent Controller in Angular?

  9. 9

    How to inject a variable from scope into an Angular controller?

  10. 10

    bind $scope in Directives/Controller

  11. 11

    bind $scope in Directives/Controller

  12. 12

    Not able to access $scope variable from within a controller function in Angular JS

  13. 13

    Angular:How to pass scope data to directives

  14. 14

    Access controller scope from directive

  15. 15

    Access the scope value from the controller

  16. 16

    How to access to the controller scope from a directive inside ng-repeat?

  17. 17

    How to access a scope from a directive in-another controller

  18. 18

    How do I access a directive scope from a parent directive/controller?

  19. 19

    Scope inheritance for angular directives

  20. 20

    angular scope in controllers + directives

  21. 21

    Scope inheritance for angular directives

  22. 22

    How to access $viewValue from controller in Angular

  23. 23

    How to call controller function from directives

  24. 24

    How to call angular controller scope function from directive with parameters?

  25. 25

    How to call angular controller scope function from directive with parameters?

  26. 26

    Angular how to change controller scope from onclick directive

  27. 27

    How to pass a function to transcluded scope from a controller (angular 1.3)?

  28. 28

    Angular: how to connect scope in the controller and scope in services?

  29. 29

    Angular Directives with External Controller

HotTag

Archive