When I use $compile
to create and bind a directive, how can I also add a variable as an attribute? The variable is an object.
var data = {
name: 'Fred'
};
var dirCode = '<my-directive data-record="data"></my-directive>';
var el = $compile(dirCode)($scope);
$element.append(el);
And myDirective
would be expecting:
...
scope: {
record: '='
},
...
I have tried doing
`var dirCode = '<my-directive data-record="' + data + '"></my-directive>';`
instead too.
var data = {}
needs to be attached to your controller scope to get two way binding. If you dont care about two way binding a hackier way is to do<directive data-record=`${JSON.stringify(data)}`></directive>
– Liebermann