作为Angularjs的新手,我需要对此问题提供一些帮助。我正在尝试将ng-click =“getGallery(57)”添加到模态选项卡。通过jQuery添加不是问题,但我意识到当我这样做时,Angular没有编译新添加的ng-click。我有一个简单的控制器
function imageGalleryCtrl ($scope, $http) {
//get gallery info on click
$scope.getGallery = function(id)
{
$http.post('/beta/images/get_images',{'id': id}).success(function(data)
{
$scope.images = data.thisGal_images;
$scope.images.galID = id;
});
};
}
我使用jQuery
添加ng-click到元素//Edit image tab to use angularjs
$('#image_edit a').attr('ng-click','getGallery(' + settings.id + ')');
如何强制Angular编译这个新添加的ng-click?
谢谢!
HTML模式:
这是单击图库名称时调用的模式。
<div class="modal fade hide modal-creator" id="imageUploader" tabindex="-1" data-focus-on="input:first">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"></button>
<h3>Create New Gallery</h3>
</div>
<div class="modal-body">
<ul id="myTab" class="nav nav-tabs">
<li id="image_home" class="">
<a href="#home" data-toggle="tab">Home</a>
</li>
<li id="image_upload" class="">
<a href="#upload" data-toggle="tab">Upload Image Gallery</a>
</li>
<li id="image_edit" class="">
<a href="#edit" data-toggle="tab">Edit Image Gallery</a>
</li>
</ul>
<div id="myTabContent" class="tab-content">
<div class="tab-pane fade in" id="home">
<?php include('create.php'); ?>
</div>
<div class="tab-pane fade" id="upload">
<?php include('upload.php'); ?>
</div>
<div class="tab-pane fade" id="edit">
<div class="span9">
<ul id="imageSort" class="gallery-container">
<li ng-repeat="image in images">
<img ng-src="http://images.onlinealbumproofing.com/imageGallery/{{image.galleryID}}/{{image.imageName}}" alt="">
{{image.orgName}}
</li>
</ul>
</div><!-- /span9 -->
</div><!-- /row -->
</div>
</div>
</div><!-- /modal-body -->
<div class="modal-footer">
<a href="javascript:;" class="btn" data-dismiss="modal">Close</a>
<a href="javascript:;" id="next" class="btn btn-primary">Next</a>
</div>
</div>
所以真的尝试以Angular的方式做这件事,经过一些研究后,我就在这里。 控制器与上面相同,我更新了模态上的选项卡,如下所示。
<div class="modal-body">
<ul id="myTab" class="nav nav-tabs">
<li id="image_home" class="">
<a href="#home" data-toggle="tab">Home</a>
</li>
<li id="image_upload" class="">
<a href="#upload" data-toggle="tab">Upload Image Gallery</a>
</li>
<li id="image_edit" class="">
<a ng-click="getGallery(57)" href="#edit" data-toggle="tab">Edit Image Gallery</a>
</li>
</ul>
如果我像上面那样硬编码ng-click,则标签会按预期更新。我想要做的是,当调用模态时,(#image_edit a)得到一个定义的ng-click =“getGallery(id)。我需要告诉Angular再次查看ng-click的id。
答案 0 :(得分:15)
虽然我也认为你应该尝试找到另一种方法,但问题的答案是
如何强制Angular编译这个新添加的ng-click?
是
// remove and reinsert the element to force angular
// to forget about the current element
$('#button1').replaceWith($('#button1'));
// change ng-click
$('#button1').attr('ng-click','exec2()');
// compile the element
$compile($('#button1'))($scope);
请参阅此工作示例:http://plnkr.co/edit/SgvQzaY0TyFHD1Mf0c3F?p=preview
答案 1 :(得分:2)
基本上我得到的是你想要实现这个目标:
<ul id="imageSort" class="gallery-container">
<li ng-repeat="image in images">
<img
ng-src="http://images.onlinealbumproofing.com/imageGallery/{{image.galleryID}}/{{image.imageName}}"
alt=""
ng-click="getGallery(image.galleryID)"
>
{{image.orgName}}
</li>
</ul>
问题是我不确定settings.id和特定图像的galleryID之间的关系。如果你可以在数据中设置这种关系,那么它就会自动绑定你的状态。