Angularjs 为动态添加的每个元素设置属性
如何为每个添加的动态按钮设置“类型属性”? 在下面的代码中,标签名称发生了完美的更改,而且我可以将“type attribute”设置为第一个添加的按钮,但其余的按钮类型没有正确更改。。你能帮我查一下并解决这个问题吗 更新:Angularjs 为动态添加的每个元素设置属性,angularjs,Angularjs,如何为每个添加的动态按钮设置“类型属性”? 在下面的代码中,标签名称发生了完美的更改,而且我可以将“type attribute”设置为第一个添加的按钮,但其余的按钮类型没有正确更改。。你能帮我查一下并解决这个问题吗 更新: var-app=angular.module('myapp',['ngSanitize']); app.controller('MainCtrl',函数($scope,$compile){ var计数器=0; $scope.buttonFields=[]; $scope.
var-app=angular.module('myapp',['ngSanitize']);
app.controller('MainCtrl',函数($scope,$compile){
var计数器=0;
$scope.buttonFields=[];
$scope.add_按钮=函数(索引){
$scope.buttonFields[计数器]={按钮:“提交”};
var buttonhtml='{{buttonFields['+counter+'].button}}//单击/';
var按钮=$compile(buttonhtml)($scope);
元素(document.getElementById('add')).append(按钮);
$scope.changeTosubmit=函数(){
var el=document.getElementById(“按钮类型”);
el.setAttribute(“类型”、“提交”);
编译(el);
};
$scope.changeToreset=函数(){
var el=document.getElementById(“按钮类型”);
el.setAttribute(“类型”、“重置”);
编译(el);
};
$scope.changeTocancel=函数(){
var el=document.getElementById(“按钮类型”);
el.setAttribute(“类型”、“取消”);
编译(el);
};
++计数器;
};
$scope.selectButton=函数(val){
$scope.buttonField=val;
$scope.showButton_Types=true;
};
});代码>
添加按钮
按钮名称(?)
更改按钮类型(?)
提交
重置
取消
您正在通过document.getElementById(“按钮类型”)选择“所有按钮”
。这就是问题所在:getElementById
返回它可以找到的具有给定id的第一个项
尝试改用document.querySelectorAll()(它将始终返回一个数组)。但我想为每个按钮添加不同类型的属性,我想设置第一个按钮“type to Submit”,第二个按钮“type to Reset”。。如果我使用document.querySelectorAll()将错误显示为document.querySelectorAll.的参数不足。。如何解决这个问题,你能帮我试试吗?你面临的核心问题是“我如何选择合适的元素”?我只能建议您学习CSS选择器的基础知识。我建议阅读本文,例如:除此之外:在您的示例中,您不应该向每个按钮添加相同的id,因为id属性应该是唯一的。尝试使用类,当然,在使用document.querySelectorAll()时,您需要将正确的选择器作为参数传递。@TrueBlueAussie Hi。。我更新了代码..抱歉@TrueBlueAussie。。我确实删除了……)