Last Updated: June 29, 2023
·
4.959K
· ydeshayes

Insert a new piece of code contain angularJS directive at runtime

You have to use $compile.

For example here, I insert new html code that contain angularjs directive (var fieldValue) in a new cell of a table.

var newCell = tblBodyObj.rows[i].insertCell(position);

$(newCell).append($compile(fieldValue)($scope));

$compile(fieldValue)($scope) return an object and you can append it to the dom.

1 Response
Add your response

Yes you're right, if you're calling this function out of a angularjs function/context, then you need to call $scope.apply() after compile.

over 1 year ago ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

AngularJS pages are long-lived browser apps with the same script risk as a newer single-page app. Third-party code in the template can read forms and values rendered on the page.

Security insights from Cside