简体   繁体   English

AngularJS添加DOM元素

[英]AngularJS add DOM element

I have a question concerning adding DOM elements with AngularJS. 我有一个关于用AngularJS添加DOM元素的问题。 I have the following example: 我有以下示例:

My Example 我的例子

When I click in a table cell, this span element: 当我单击表格单元格时,此span元素:

<span editable-text="serviceSchedule.startTime">HH:mm</span>

should be added to clicked table cell. 应该添加到单击的表格单元格中。 I have tried a lot but with no success. 我已经尝试了很多,但是没有成功。 I would be very thankful for help! 我将非常感谢您的帮助!

Rather than directly manipulating the DOM with a new element, I would suggest modifying your model when cells are clicked and let the bindings do the work in the DOM. 我建议不要在单击单元格时修改模型并让绑定在DOM中起作用,而不是直接使用新元素来操作DOM。 This is much more in line with the patterns used in AngularJS or any other MV* framework. 这与AngularJS或任何其他MV *框架中使用的模式更加一致。

Here is a simplified example based on your original code. 这是一个基于原始代码的简化示例。

 var myApp = angular.module('myApp', []); myApp.controller('MyController', ['$scope', function($scope) { //Data $scope.names = [ { id: 1, value: 'Hans Meier' }, { id: 2, value: 'Walter Mueller' }]; $scope.labels = [ { value: 'Montag: 21.09.2015', scheduleContainerCount: { 1: 0, 2: 0 } }, { value: 'Dienstag: 22.09.2015', scheduleContainerCount: { 1: 0, 2: 0 } }, { value: 'Mittwoch: 23.09.2015', scheduleContainerCount: { 1: 0, 2: 0 } }]; //Functions $scope.addScheduleContainer = function (name, label) { label.scheduleContainerCount[name.id]++; }; $scope.getRange = function (name, label) { return new Array(label.scheduleContainerCount[name.id]); }; }]); 
 td { border: 1px solid black; } table { border-collapse: collapse; } td > span { display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <table ng-app="myApp" ng-controller="MyController"> <thead> <tr> <td></td> <td ng-repeat="label in labels">{{ label.value }}</td> </tr> </thead> <tbody> <tr ng-repeat="name in names"> <td>{{ name.value }}</td> <td ng-repeat="label in labels" ng-click="addScheduleContainer(name, label)"> <span ng-repeat="i in getRange(name, label) track by $index">HH:mm</span> </td> </tr> </tbody> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM