gpt4 book ai didi

javascript - 将输入值绑定(bind)到 ng-click 按钮(将值作为参数发送)

转载 作者:太空狗 更新时间:2023-10-29 13:56:55 24 4
gpt4 key购买 nike

我正在尝试将输入字段的值绑定(bind)到 ng-click 上我的方法的参数。这是我得到的,但它不起作用,我不太确定是否可以这样做?:

<input type="text" name="name" value="{{post.PostId}}" />
<button ng-click="getById(post.PostId)"></button>
<h1>{{post.Title}}</h1>


$scope.getById = function (id) {
console.log(id);
return $http.get('/api/Post/' + id);
}

最佳答案

您应该为输入元素使用 ng-model 指令。

标记

 <input type="text" name="name" ng-model="post.PostId" />
<button ng-click="getById(post.PostId)"></button>
<h1>{{post.Title}}</h1>

这将处理绑定(bind)到您的属性 post.PostId 的 2-way 模型。您的 ng-click 指令将选取输入元素中输入的正确值。

查看我的工作 Plunk :)

关于javascript - 将输入值绑定(bind)到 ng-click 按钮(将值作为参数发送),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31233876/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com