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?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside