我正在努力使用 Angularjs 中的观察器来控制带有跨度值列表的数组对象。
它部分工作,当我输入跨度元素时,会自动为每个跨度创建一个数组,当我删除任何跨度元素时 -> 现有数组中的相应行将被删除,所有其他行将正确重新对齐(不会干扰值和名称)。
问题是当我删除一个 span 元素并使用输入文本重新输入它时,它没有添加到我的数组中。因此,删除一个跨度元素后,然后输入任何新元素 - 这些新值不会附加到我的数组中。
演示代码 fiddle link
我的代码中缺少什么?
如何在不影响剩余行的值(数组的名称和值)的情况下重新插入要附加到现有数组对象的跨度?
请注意,图表中的值会随时更改。
这是我正在使用的代码:
<script>
function rdCtrl($scope) {
$scope.dataset_v1 = {};
$scope.dataset_wc = {};
$scope.$watch('dataset_wc', function (newVal) {
//alert('columns changed :: ' + JSON.stringify($scope.dataset_wc, null, 2));
$('#status').html(JSON.stringify($scope.dataset_wc));
}, true);
$(function () {
$('#tags input').on('focusout', function () {
var txt = this.value.replace(/[^a-zA-Z0-9\+\-\.\#]/g, ''); // allowed characters
if (txt) {
//alert(txt);
$(this).before('<span class="tag">' + txt.toLowerCase() + '</span>');
var div = $("#tags");
var spans = div.find("span");
spans.each(function (i, elem) { // loop over each spans
$scope.dataset_v1["d" + i] = { // add the key for each object results in "d0, d1..n"
id: i, // gives the id as "0,1,2.....n"
name: $(elem).text(), // push the text of the span in the loop
value: 3
}
});
$("#assign").click();
}
this.value = "";
}).on('keyup', function (e) {
// if: comma,enter (delimit more keyCodes with | pipe)
if (/(188|13)/.test(e.which)) $(this).focusout();
if ($('#tags span').length == 7) {
document.getElementById('inptags').style.display = 'none';
}
});
$('#tags').on('click', '.tag', function () {
var tagrm = this.innerHTML;
sk1 = $scope.dataset_wc;
removeparent(sk1);
filter($scope.dataset_v1, tagrm, 0);
$(this).remove();
document.getElementById('inptags').style.display = 'block';
$("#assign").click();
});
});
$scope.assign = function () {
$scope.dataset_wc = $scope.dataset_v1;
};
function filter(arr, m, i) {
if (i < arr.length) {
if (arr[i].name === m) {
arr.splice(i, 1);
arr.forEach(function (val, index) {
val.id = index
});
return arr
} else {
return filter(arr, m, i + 1)
}
} else {
return m + " not found in array"
}
}
function removeparent(d1)
{
dataset = d1;
d_sk = [];
Object.keys(dataset).forEach(function (key) {
// Get the value from the object
var value = dataset[key].value;
d_sk.push(dataset[key]);
});
$scope.dataset_v1 = d_sk;
}
}
</script>
最佳答案
使用 Angular 我认为你会想要更多关于 html 方面的东西,特别是使用 ng-repeat 和 ng-click 和 ng-model 首先你会想要创建你的数组,这可以简单地在你的代码文件中完成或使用 ng-init。这是一个例子
(function(){
var app=angular.module('demoApp',[]);
app.controller('demoApp',[function(){
this.spans=[""];
this.currentSpan='';
this.addSpan=function(){
this.spans.push(this.currentSpan);
this.currentSpan='';
};
}]);
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='demoApp'>
<div ng-controller='demoApp as demo'>
<span ng-repeat='span in demo.spans track by $index' ng-click='demo.spans.splice($index,1)'>{{span}}</span><span>{{demo.currentSpan}}</span>
<textarea ng-model='demo.currentSpan' placeholder='new text'></textarea>
<button ng-click='demo.addSpan()' >add</button>
</div>
</div>
关于javascript - 附加到通过 Angularjs 中的输入文本添加的新 span 元素的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34148913/