gpt4 book ai didi

javascript - 如何在 angularJS 中访问 JavaScript 中的 HTML 变量

转载 作者:行者123 更新时间:2023-12-03 09:46:37 24 4
gpt4 key购买 nike

我正在 angularJS 中填写一个选择列表。这是代码。

<select class="form-control" ng-model="NewProduct.category" ng-options="category.id as category.name for category in Categories | orderBy:['name']" ng-change="update(NewProduct.category)" required></select>

更改时调用更新函数;打印类别ID。

$scope.update = function (value) {
alert ($scope.NewProduct.category);
};

但我想在此更新函数中访问categoryID 和categoryName。我尝试过:

$scope.update = function (value) {
alert ($scope.NewProduct.category.id);
alert ($scope.NewProduct.category.name);

};

但它警告未定义,未定义。

如何在此更新函数中获取categoryID 和categoryName。

最佳答案

将您的 html 更改为:

<select class="form-control" ng-model="NewProduct.category" ng-options="category as category.name for category in Categories | orderBy:['name']" ng-change="update(NewProduct.category)" required></select>

这会将整个类别存储在 NewProduct.category 中,而不仅仅是 id

关于javascript - 如何在 angularJS 中访问 JavaScript 中的 HTML 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31005296/

24 4 0
文章推荐: javascript - 如何使用JS读取文本文件中的数据并按时间间隔动态更新
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com