Javascript 更改从单个源动态添加的类似ng重复值
我想分别从显示的单个源代码中更改动态添加的类似ng repeat值,您可以从下面的代码中看到,如果单击动态添加的div,它将在右侧显示一个表单,如果我们可以添加输入文本字段,但我的问题是,当我试图添加或更改它绑定并添加到所有div的文本时,我只想将输入字段添加到所选div 请不要使用ng repeat方法推送“再次添加”按钮的值,从append方法开始,只需添加Javascript 更改从单个源动态添加的类似ng重复值,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,我想分别从显示的单个源代码中更改动态添加的类似ng repeat值,您可以从下面的代码中看到,如果单击动态添加的div,它将在右侧显示一个表单,如果我们可以添加输入文本字段,但我的问题是,当我试图添加或更改它绑定并添加到所有div的文本时,我只想将输入字段添加到所选div 请不要使用ng repeat方法推送“再次添加”按钮的值,从append方法开始,只需添加 var-app=angular.module('myapp',['ngSanitize']); app.controller('Ad
var-app=angular.module('myapp',['ngSanitize']);
app.controller('AddCtrl',函数($scope,$compile){
$scope.items=[];
$scope.add_New=函数(索引){
var itemhtml='//单击此处//{{$index+1}}。{{{item.name}};
var item=$compile(itemhtml)($scope);
元素(document.getElementById('drop')).append(item);
};
$scope.add=函数(){
$scope.items.push({
姓名:“你好”
});
};
$scope.select=函数(){
$scope.showItem=true;
}
});代码>
。添加{
位置:绝对;高度:自动;宽度:200px;左侧:0;右侧:自动;顶部:0;底部:0;
}
.表演{
位置:绝对;宽度:自动;左侧:200px;右侧:0;顶部:0;底部:0;
边框:1px纯红;
浮动:对;
}
.内容{
边框:1px纯蓝色;
}
再加上
{{$index+1}}。
添加
这是因为您对所有主要项目和子项目使用相同的列表。
因为您不想使用ngrepeat,所以必须为所有主要项目创建索引
创建一个主列表。每次创建主项时,都会增加索引
之后,每个主项都有一个值列表(子项)
希望是你所期望的
工作代码如下:
app.controller('AddCtrl', function ($scope, $compile) {
$scope.items = [];
$scope.index = 0;
$scope.currentIndex = -1;
$scope.add_New = function (index) {
var itemhtml = '<div ng-click="select(' + $scope.index + ')" class="content">//click here first// <div ng-repeat="item in items[' + $scope.index + '].values">{{$index + 1}}. {{item.name}}</div></div>';
var item = $compile(itemhtml)($scope);
angular.element(document.getElementById('drop')).append(item);
$scope.items[$scope.index] = {};
$scope.index++;
};
$scope.add = function () {
if ($scope.items[$scope.currentIndex].values == undefined)
$scope.items[$scope.currentIndex].values = [];
$scope.items[$scope.currentIndex].values.push({
name: "hi"
});
};
$scope.select = function(index){
$scope.showItem = true;
$scope.currentIndex = index;
}
});
<body ng-controller="AddCtrl">
<div class="add"><button ng-click="add_New()">add Again</button>
<div id="drop">
</div>
</div>
<div ng-show="showItem" class="show">
<div ng-repeat="item in items[currentIndex].values">
{{$index + 1}}.<input type="text" ng-model="item.name">
</div>
<button ng-click="add()">Add</button>
</div>
</body>
app.controller('AddCtrl',函数($scope,$compile){
$scope.items=[];
$scope.index=0;
$scope.currentIndex=-1;
$scope.add_New=函数(索引){
var itemhtml='//首先单击此处//{{$index+1}}。{{{item.name}};
var item=$compile(itemhtml)($scope);
元素(document.getElementById('drop')).append(item);
$scope.items[$scope.index]={};
$scope.index++;
};
$scope.add=函数(){
如果($scope.items[$scope.currentIndex].values==未定义)
$scope.items[$scope.currentIndex].values=[];
$scope.items[$scope.currentIndex].values.push({
姓名:“你好”
});
};
$scope.select=函数(索引){
$scope.showItem=true;
$scope.currentIndex=索引;
}
});
再加上
{{$index+1}}。
添加
非常感谢……)我的预期完全正确……:)你做得很好,你很认真。。。