qhljia
Last Updated: February 25, 2016
·
4.49K
· 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.

2 Responses
Add your response

8034

You also need to mention that is some cases there also is a need to call $scope.apply() after compile.

over 1 year ago ·
8045

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 ·