btm*_*ach 7 html javascript angularjs angularjs-ng-click angularjs-ng-model
我正在尝试将输入字段中的值绑定到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);
}
Run Code Online (Sandbox Code Playgroud)
JDT*_*LH9 21
您应该使用ng-model指令作为输入元素.
标记
<input type="text" name="name" ng-model="post.PostId" />
<button ng-click="getById(post.PostId)"></button>
<h1>{{post.Title}}</h1>
Run Code Online (Sandbox Code Playgroud)
这将处理与您的财产绑定的双向模型post.PostId.您的ng-click指令将获取输入元素中输入的正确值.
看我的工作Plunk :)