2

I am confused as to why the following Angular directive is not working:

JS

angular
    .module("app")
    .directive("symbolImage", function () {
        return {
            restrict: 'AE',
            scope: {
                width: "@",
                height: "@",
                symbol: "@"
            },
            template: "<div>{{width}} {{height}} {{symbol|json}}</div>",
            replace: true
        };
    });

VARIABLES

$scope.current = {prop1:{foo:"bar"}, prop2:{foo2:"bar2"}};
$scope.properties = ["prop1", "prop2"];

HTML

<tr ng-repeat="prop in properties">
    <td>
        <symbol-image height="20" width="20" symbol="current[prop]"/>
    </td>
</tr>

EXPECTED OUTPUT (First Repeat)

20 20 {foo:"bar"}

ACTUAL OUTPUT (First Repeat)

20 20 "current[prop.key]"

It is (was) my understanding the the values passed to scope are evaluated but this does not seem to be the case.

Any/all help is appreciated.

2
  • change symbol: "@" to symbol: "=" Commented Jun 14, 2016 at 14:36
  • Thanks! It worked. Commented Jun 14, 2016 at 14:39

2 Answers 2

2

You just need to use

    <symbol-image height="20" width="20" symbol="{{current[prop]}}"/>

'@' expects you to give the directive a string. '=' expects you to give the directive an object.

Another way to solve you issue would have been to define your directive like this :

        scope: {
            width: "@",
            height: "@",
            symbol: "="
        },
Sign up to request clarification or add additional context in comments.

Comments

0

you should declarate your props this way

        scope: {
            width: "@",
            height: "@",
            symbol: "="
        },

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.