Can i send an object to isolated scope '@' property

86 views Asked by At

I have a directive which accepts object as a parameter (attribute). I am getting the data in my link function as string. Is there any way to receive the data as object keeping @ in the attribute and without using eval or JSON.parse() in link.

Directive code:

module.directive('example', function () {
    return {
        scope: {
            object: '@'
        }
        link: function ($scope) {
            console.log($scope.object);
        }
    }
});

HTML code:

<example object="{{sampleObject}}"></example>

Controller code:

module.controller('exampleCtrl', function ($scope) {
    $scope.sampleObject = {
        name: 'name',
        width: 100,
        height: 100
    };
})
1

There are 1 answers

0
Magomogo On

No, you can't. Result of "@" binding is always a string, since this way you bind the value of DOM attribute. See the documentation.

If you are searching a way to avoid two-way data binding, use "&" expression.