我正在为每个项目动态创建文本框和保存按钮,单击保存按钮后,我需要从该特定项目的文本框中获取值。
for (let d = 0; d <= ItemsInfo.length - 1; d++)
{
content += '<tr> <td> <label for="lblPriority">Item Priority </label> </td> ';
content += ' <td> <input type="text" id="inpItemPRIORITY" ng-model="prty" value=" ' + ItemsInfo[d].PRIORITY + ' " /> </td> </tr>';
content += '<tr> <td> <label for="lblItemComment">Item Comment</label></td> ';
content += ' <td> <input type="text" id="inpItemCOMMENT" ng-model="cmnt" value=" ' + ItemsInfo[d].COMMENT + ' " /> </td> </tr>';
// Save Item
content += '<tr> <td> <button class="get-data" ng-click="buttonClick(prty,cmnt)">Save Item(' + ItemsInfo[d].ITEM_ID + ')</button> </td> </tr> ';
}
在控制器中:
$scope.buttonClick = function (prty,cmnt) {
console.log(prty + " " + cmnt); } // console.log displays as undefined undefined
还是有更好的方法来做到这一点?
首先,正确的AngularJs应该是直接在HTML模板中使用ngRepeat,但是我仍然会为您提供当前代码的解决方案:
首先,HTML ID应该是唯一的,您正在控制器中的数组上手动循环,因此您可以在ID中添加索引号,并将其赋予函数以查找元素(还修复了for循环条件以进行优化) ):
在这种情况下,绑定到模型是没有用的,否则所有输入将使用相同的作用域变量。此代码还显示了在AngularJS中以jQlite格式检索DOM元素的正确方法:
for (let d = 0; d < ItemsInfo.length; d++)
{
content += '<tr> <td> <label for="lblPriority">Item Priority </label> </td> ';
content += ' <td> <input type="text" id="inpItemPRIORITY_' + d + '" value="' + ItemsInfo[d].PRIORITY + '" /> </td> </tr>';
content += '<tr> <td> <label for="lblItemComment">Item Comment</label></td> ';
content += ' <td> <input type="text" id="inpItemCOMMENT_' + d + '" value="' + ItemsInfo[d].COMMENT + '" /> </td> </tr>';
// Save Item
content += '<tr> <td> <button class="get-data" ng-click="buttonClick(' + d + ')">Save Item(' + ItemsInfo[d].ITEM_ID + ')</button> </td> </tr> ';
}
$scope.buttonClick = function (index) {
let prty = angular.element( document.querySelector( '#inpItemPRIORITY_' + index ) ),
cmnt = angular.element( document.querySelector( '#inpItemCOMMENT_' + index ) );
console.log(prty.val() + " " + cmnt.val());
};
这是一个演示片段。只是显示其工作原理的示例(EDIT:添加了$compile
指令以buttonClick
使其正常工作)。
angular.module('selectExample', [])
.controller('ExampleController', ['$scope', function($scope) {
const ItemsInfo = [
{ ITEM_ID: 'a1', COMMENT: 'comment a1', PRIORITY: 0 },
{ ITEM_ID: 'b1', COMMENT: 'comment b1', PRIORITY: 1 },
{ ITEM_ID: 'c1', COMMENT: 'comment c1', PRIORITY: 2 },
];
let content = '';
for (let d = 0; d < ItemsInfo.length; d++)
{
content += '<tr> <td> <label for="lblPriority">Item Priority </label> </td> ';
content += ' <td> <input type="text" id="inpItemPRIORITY_' + d + '" value=" ' + ItemsInfo[d].PRIORITY + ' " /> </td> </tr>';
content += '<tr> <td> <label for="lblItemComment">Item Comment</label></td> ';
content += ' <td> <input type="text" id="inpItemCOMMENT_' + d + '" value=" ' + ItemsInfo[d].COMMENT + ' " /> </td> </tr>';
// Save Item
content += '<tr> <td> <button class="get-data" ng-click="buttonClick(' + d + ')">Save Item(' + ItemsInfo[d].ITEM_ID + ')</button> </td> </tr> ';
}
$scope.htmlContent = content;
$scope.buttonClick = function (index) {
let prty = angular.element( document.querySelector( '#inpItemPRIORITY_' + index ) ),
cmnt = angular.element( document.querySelector( '#inpItemCOMMENT_' + index ) );
console.log(prty.val() + " " + cmnt.val());
};
}])
.directive('bindHtmlCompile', ['$compile', function ($compile) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
scope.$watch(function () {
return scope.$eval(attrs.bindHtmlCompile);
}, function (value) {
element.html(value);
$compile(element.contents())(scope);
});
}
};
}]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular-sanitize.min.js"></script>
<div ng-app="selectExample" ng-controller="ExampleController">
<table bind-html-compile="htmlContent"></table>
</div>
我尝试过,但是在我的条件下,如果有可用数据,则不需要隐藏其他与项目相关的内容,就需要显示一些与项目相关的内容。使用ngRepeat我无法检查检查数据的条件,因此无法动态创建文本框。
在按照您的代码进行更改之后,我在console.log中以[[object HTMLInputElement]]的形式获取了味精
是的,并且.value()收到作为类型错误的错误消息:cmnt.value不是函数。
console.log(cmnt.value); ->未定义
您可以称我为愚蠢的人,忘记了我的基础知识,它
.val()
不是.value()
jQuery中的。尝试val()
;)