我正在使用Symfony2和AngularJs
我用jquery创建了一个像symfony2文档的表单集合,并且我已经为动态创建的每个输入分配了ng-model,但是当我提交表单时,Angularjs Dom没有检测到创建的动态ng模型! 我试过$ compile和$ apply但是我没有结果 请帮忙 我的代码:
<script>
var $collectionHolder;
// setup an "add a guest" link
var $addGuestLink = $('<a href="#" class="add_guest_link">Add a guest</a>');
var $newLinkLi = $('<li></li>').append($addGuestLink);
jQuery(document).ready(function () {
// Get the ul that holds the collection of guests
$collectionHolder = $('ul.guests');
// add a delete link to all of the existing tag form li elements
$collectionHolder.find('li').each(function () {
addGuestFormDeleteLink($(this));
});
// add the "add a guest" anchor and li to the guests ul
$collectionHolder.append($newLinkLi);
// count the current form inputs we have (e.g. 2), use that as the new
// index when inserting a new item (e.g. 2)
$collectionHolder.data('index', $collectionHolder.find(':input').length);
$addGuestLink.on('click', function (e) {
// prevent the link from creating a "#" on the URL
e.preventDefault();
// add a new guest form (see next code block)
addGuestForm($collectionHolder, $newLinkLi);
var $scope = angular.element('#expenseCtrl').scope();
$scope.apply(angular.element('#expenseCtrl')
.injector().get('$compile')(addGuestForm($collectionHolder, $newLinkLi))('$scope'));
});
});
function addGuestForm($collectionHolder, $newLinkLi) {
// Get the data-prototype explained earlier
var prototype = $collectionHolder.data('prototype');
// get the new index
var index = $collectionHolder.data('index');
// Replace '__name__' in the prototype's HTML to
// instead be a number based on how many items we have
var newForm = prototype.replace(/__name__/g, index);
$collectionHolder.data('index', $collectionHolder.children().length);
// Display the form in the page in an li, before the "Add a guest" link li
var $newFormLi = $('<li></li>').append(newForm);
$newLinkLi.before($newFormLi);
// add a delete link to the new form
addGuestFormDeleteLink($newFormLi);
}
function addGuestFormDeleteLink($guestFormLi) {
var $removeFormA = $('<a href="#">delete this guest</a>');
$guestFormLi.append($removeFormA);
$removeFormA.on('click', function (e) {
// prevent the link from creating a "#" on the URL
e.preventDefault();
// remove the li for the guest form
$guestFormLi.remove();
});
}
</script>