单击后动态更改按钮上的名称 [英] Dynamically changing the name on button after click
问题描述
我已经使用$ http.get api从数据库中检索了内容,并使用 ng-repeat
显示了每条记录.对于每条记录,我都有一个 like和comment按钮.
在单击like按钮时,im使用$ http.post插入一条记录,并且我必须将按钮的名称更改为喜欢"
.问题是-单击任何类似按钮的记录后,所有其他记录(例如按钮名称)都将更改为喜欢",包括单击的按钮.
I have retrieving the contents from database using $http.get api and displaying each record using ng-repeat
. For each record i have a like and comment button.
On clicking on like button, i m inserting a record using $http.post and i have to change the name of button to "Liked"
.The problem is- on clicking any one of the record like button, all other records like button name changes to Liked including clicked button.
<div ng-repeat="dat in details | filter : { product_name : textname} as results">
<p style="color:#4C97C8;" class="lead"><strong>{{dat.summary}}</strong></p>
<ul>
<li><b>Product:</b><span> {{dat.product_name}}</span></li>
<li><b>Product Manager:</b><span> {{dat.first_name}} {{dat.last_name}}</span></li>
<li><b>Description:</b><span> {{dat.description}}</span></li>
</ul>
<button style="background-color:#4C97C8;color:white;height:30px" class="btn buttonlike" ng-click="likebtn(dat.id,loginname)"><span class="glyphicon glyphicon-hand-right"></span><strong> {{ likebtnname }}</strong></button>
<button style="background-color:#4C97C8;color:white;height:30px" class="btn" ng-disabled="comment" ng-click="comment=true"><span class="glyphicon glyphicon-comment"></span><strong> Comment</strong></button>
<div class="input-group " ng-show="comment">
<input type="text" id="commentarea" name="commentarea" class="form-control" placeholder="comment" aria-describedby="basic-addon2" ng-model="takecomment">
<span class="input-group-addon" id="basic-addon2" ng-click="takecomment=mycomment(dat.id,takecomment)"><span class="glyphicon glyphicon-send"></span></span>
</div>
</div>
脚本/控制器中的
likebtn()方法是
likebtn() method inside script/controller is
$scope.likebtnname="Like";
$scope.likebtn = function(idvalue,namevalue) {
var likereque = {
method: 'POST',
url: "https://url/Likes",
headers: {
"Content-Type": "application/json"
},
data: {
"platform": {
"record": { "idea_record": idvalue,
"liker": $scope.loginname
}
}
}
}
$http(likereque).then(function(){
alert("Liked Successfully");
$scope.likebtnname="Liked"; },
function(){alert("try again");});
}
如何仅更改单击按钮的名称?
How can i change the name of clicked button only?
推荐答案
HTML:
<button style="background-color:#4C97C8;color:white;height:30px" class="btn buttonlike" ng-click="$parent.likebtn(dat.id,loginname, dat)"><span class="glyphicon glyphicon-hand-right"></span><strong> {{ dat.likebtnname||'Like' }}</strong></button>
Javascript:
$scope.likebtn = function(idvalue,namevalue,dat) {
var likereque = {
method: 'POST',
url: "https://url/Likes",
headers: {
"Content-Type": "application/json"
},
data: {
"platform": {
"record": { "idea_record": idvalue,
"liker": $scope.loginname
}
}
}
}
$http(likereque).then(function(){
alert("Liked Successfully");
dat.likebtnname="Liked"; },
function(){alert("try again");});
}
这篇关于单击后动态更改按钮上的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!