[英]How to pass an angular scope object to a newly created DOM
I have an angular object(model) created in controller. 我在控制器中创建了一个有角度的对象(模型)。
$scope.deletedres = [];
I am trying to append a new DOM to the html body along with the angular object(modal) as shown below. 我试图将新的DOM连同角对象(模态)一起添加到html主体,如下所示。
$('body').append('<span>'+restaurant.name+' have been removed.</span><a class="btn-flat yellow-text" href="#"; ng-click="addRestaurant($scope.deletedres[$scope.deletedres.length-1])">Undo<a>');
When I view it with google chrome dev tools, it shows that $scope.deletedres
as [object Object] and addRestaurant() function receive nothing. 当我使用Google chrome开发工具查看它时,它表明$scope.deletedres
作为[object Object]和addRestaurant()函数什么都没收到。
Can anyone enlighten me on this issue? 谁能在这个问题上启发我?
Is there any other ways to reference/pass an angular modal to a newly created DOM? 还有其他方法可以将角度模态参考/传递给新创建的DOM吗?
The way you are adding the DOM is wrong. 您添加DOM的方式是错误的。 Add the html inside the scope of controller. 在控制器范围内添加html。 Use ng-show to show or hide the dom. 使用ng-show显示或隐藏dom。 JQuery is not necessary. 不需要JQuery。 Example 例
<span ng-show="restaurant.delete">{{restaurant.name}} have been removed.</span>
<a class="btn-flat yellow-text" href="#"; ng-click="restaurant.delete=false">Undo<a>
This is just an example you can improve on 这只是您可以改进的一个示例
When you use jQuery to add fragments of HTML there is no way for angular to parse it. 当您使用jQuery添加HTML片段时,无法通过角度解析它。 Thats the reason your angular code inside the html is working. 这就是为什么您的html内的角度代码正常工作的原因。
You can use $compile service. 您可以使用$ compile服务。
var html = '<span>{{restaurant.name}} have been removed.</span><a class="btn-flat yellow-text" href="#"; ng-click="addRestaurant(deletedres[deletedres.length-1])">Undo</a>';
var linkFn = $compile(html);
var content = linkFn(scope);
$('body').append(content);
Still as noted by Harish it's wrong. 正如Harish指出的那样,这是错误的。 All manipulations with DOM must be done in directives. 所有使用DOM的操作都必须在指令中完成。 You can create directive that will be responsible for showing some message (or custom html template) on button click. 您可以创建指令,该指令负责在单击按钮时显示一些消息(或自定义html模板)。
Dmitry Bezzubenkov is right. 德米特里·别祖本科夫 ( Dmitry Bezzubenkov)是对的。 If you want to manipulate DOM with Angular, you should do that with your custom directive, rather than do that in your controller directly. 如果要使用Angular操作DOM,则应使用自定义指令执行此操作,而不是直接在控制器中执行。 And to do so, you may refer to $compile
service. 为此,您可以参考$compile
服务。 Here's the official document for that. 这是该文件的正式文件 。
However, in your case, I believe what you actually want to do is remove the item from a list while enable the item to be recovered from deletion. 但是,对于您而言,我相信您真正想做的是从列表中删除该项目,同时使该项目能够从删除中恢复。 In this sense, you may try this approach with Angular: 从这个意义上讲,您可以在Angular中尝试这种方法:
$scope.res
and $scope.deletedres
) (假设$scope.res
和$scope.deletedres
) ng-click
. 注册一个删除功能, ng-click
使用ng-click
将其绑定到删除按钮。 In this function, you will remove the item from $scope.res
and then push the item to $scope.deletedres
在此功能中,您将从$scope.res
删除该项目,然后将其推送到$scope.deletedres
$scope.deletedres
to $scope.res
. 也就是说,将项目从$scope.deletedres
移至$scope.res
。 Bind this item to UNDO
text in your message box. 将此项目绑定到消息框中的UNDO
文本。 ng-repeat
to show your $scope.res
list in the main container, and $scope.deletedres
in the message box container. 使用ng-repeat
在主容器中显示$scope.res
列表,在消息框容器中显示$scope.deletedres
。 It would be something like this: 就像这样:
angular
.module('modelTest', [])
.controller('MainCtrl', function($scope) {
$scope.res = [
{id: 1, name: 'Restaurant1'},
{id: 2, name: 'Restaurant2'},
{id: 3, name: 'Restaurant3'}
];
$scope.deletedres = [];
$scope.delete = function(id) {
var item, obj, i, j;
for(i = 0, j = $scope.res.length; i < j; i++) {
obj = $scope.res[i];
if(obj.id === id) {
$scope.deletedres.push(obj);
$scope.res.splice(i, 1);
}
}
};
$scope.undo = function(id) {
var item, obj, i, j;
for(i = 0, j = $scope.deletedres.length; i < j; i++) {
obj = $scope.deletedres[i];
if(obj.id === id) {
$scope.res.push(obj);
$scope.deletedres.splice(i, 1);
}
}
}
});
Here's the sample code . 这是示例代码 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.