1

could someone please explain me the difference between these two piece of code, and the way to manage it.

Here is my index.html

<body ng-app="docsIsolateScopeDirective">
  <div ng-controller="Controller">
  <my-customer info="naomi" ></my-customer>
  {{nameUser}}
  <my-customer info="igor"></my-customer>
</div>
</body>

Here is my script.js

(function(angular) {
  'use strict';
angular.module('docsIsolateScopeDirective', [])
  .controller('Controller', ['$scope', function($scope) {
    $scope.nameUser = 'naomi';
    $scope.naomi = { name: 'Naomi', address: '1600 Amphitheatre' };
    $scope.igor = { name: 'Igor', address: '123 Somewhere' };
  }])
  .directive('myCustomer', function() {
    return {
      restrict: 'E',
      scope: {
        customerInfo: '=info'
      },
      templateUrl: 'my-customer-iso.html'
    };
  });
})(window.angular);

And finally my my-customer-iso.html :

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

The result is this :

Name: Naomi Address: 1600 Amphitheatre
naomi Name: Igor Address: 123 Somewhere

Question is : I would like to set the info of my markup with a scope variable (basically $scope.nameUser which contains "naomi"). But when I write

<my-customer info={{nameUser}} </my-customer> // or <my-customer info="{{nameUser}}"> </my-customer> 

instead of

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

the code is broken and I don't understand why.. Is the scope variable can't be consider like a string ? Thanks

EDIT : Even if I write :

<my-customer info = nameUser> </my-customer>

The result is still different. I don't receive an error but the output is :

Name: Address:
Name: Igor Address: 123 Somewhere

So I think that I'm missing something about the interaction between the controller and the directive. See here : https://plnkr.co/edit/hbi4whoH3Dj9lDV3vVzZ?p=preview

3 Answers 3

2

Instead of <my-customer info={{nameUser}} </my-customer> just use <my-customer info="nameUser" </my-customer>. You don't need the {{ }}

UPDATE:

I'm not quite sure what you are trying to achieve. In your example, you are trying to pass a simple string 'naomi', but your directive expects an object with the following structure:

customerInfo = {
   name: "",
   address: ""
};

So, if you want to have it working, you should make your scope object look like this:

$scope.nameUser = { name: 'XXX', address: 'YYY' };

and then pass it to the directive like so:

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

Take a look at this plunker.

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

4 Comments

Thank you for your answer but it doesn't work. I don't receive an error, but the result is "Name: Address:"
I've updated my answer, could please please clarify this for me?
Ok I understand my mistake now. I thought "naomi" were a string that refers to $scope.naomi and not the object itself. Thank you for your explanation
I'm glad I was able to help you :)
1

There is an important difference here to note. In your directive you use "=info" for scope customerInfo attribute assignment in your directive. The = means assignment by reference. Therefore you cannot use {{}} as it is an evaluation expression. So for example, if you have used

customerInfo: '@info'

your code would work fine. Because '@' binds property to the evaluated value instead. Or, otherwise, just remove {{}} evaluation expression from <my-customer info={{nameUser}} </my-customer> to be

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

In addition, the empty output you receive, is because $scope.nameUser is a string, but you are expecting an object with name and address attributes in your directive.

2 Comments

Thank you for your answer. Please have a look on the edit
Ok I understand my mistake now. I thought "naomi" were a string that refers to $scope.naomi and not the object itself. Thank you for your explanation
0

When assigning isolated scopes, the value passed (info="naomi") reflects an assigned property of the parent scope. The interpolation handlebars will not work here. Drop off the handlebars, leaving 'info="nameUser"' should do the trick. Just remember that nameUser is just a string while naomi was an object. So naomi.name yielded a string but nameUser.name doesn't work.

1 Comment

Thank you for your answer. If I'm right, the property "nameUser" comes from the parent scope . So how can I solve this ?

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.