[英]ng-click event not working
Angular JS新手在這里。 我正在嘗試對未構建的網站進行基本更改。
我在數據庫中的表上添加了一列,稱為display_appraisal。 我希望它與表上稱為display的列相同。
我從字面上復制了顯示函數的代碼,並將其從html文件更改為display_appraisal,如下所示:
<button class="btn btn-mini" ng-class="{'btn-success': manufacturer.display==1, 'btn-danger': manufacturer.display!=1}" ng-click="manufacturers.change_display($index)"><i class="icon-white" ng-class="{'icon-ok': manufacturer.display==1, 'icon-remove': manufacturer.display!=1}"></i></button>
<button class="btn btn-mini" ng-class="{'btn-success': manufacturer.display_appraisal==1, 'btn-danger': manufacturer.display_appraisal!=1}" ng-click="manufacturers.change_display_appraisal($index)"><i class="icon-white" ng-class="{'icon-ok': manufacturer.display_appraisal==1, 'icon-remove': manufacturer.display_appraisal!=1}"></i></button>
然后在我的ctrl文件中:
change_display: function(index) {
this.list[index].display = (0 == this.list[index].display) ? 1: 0;
this.update(index, 'display');
},
change_display_appraisal: function(index) {
this.list[index].display_appraisal = (0 == this.list[index].display_appraisal) ? 1: 0;
this.update(index, 'display_appraisal');
},
按鈕正確顯示了表格中的值(成功表示1,危險表示1)。 所以我知道我正確地提取了數據。 但是由於某些原因,ng-click無效。 我還添加了一個文本框,可以將值從0更改為1,並且可以正常工作。
<input hv-blur ng-change="manufacturers.update($index,'display_appraisal')" placeholder="display_appraisal" type="text" ng-model="manufacturer.display_appraisal">
有任何想法嗎?
這里有兩種方法可以滿足您的需求。
首先,我創建一個假設與您的原因匹配的數據,但您未提供manufaturers數組。 所以。
我提出了兩種方法可以更改按鈕的類。
第一種是使用ng類,例如{'btn-success': manufacturer.display, 'btn-danger': !manufacturer.display}
然后ng單擊 "change_display($index)"
display_appraisal
第二種替代方法可以這樣
ng-class="{true:'btn-success', false:'btn-danger'
[manufacturer.display_appraisal]" ng-click="manufacturer.display_appraisal =
!manufacturer.display_appraisal
無需使用函數即可更改display_apprasial屬性。
檢查樣本以獲取更多詳細信息。
腳本
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.change_display = function(index) {
console.log(index);
$scope.data[index].display = (false == $scope.data[index].display) ? true: false;
};
$scope.data = [
{
name:'one',
display_appraisal : true,
display : true
},
{
name:'two',
display_appraisal : false,
display : true
},
{
name:'three',
display_appraisal : false,
display : false
},
{
name:'four',
display_appraisal : true,
display : false
},
{
name:'five',
display_appraisal : false,
display : false
}
]
});
的HTML
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script data-require="angular.js@1.5.x" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js" data-semver="1.5.11"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
<table class="table table-stripped">
<thead>
<tr>
<th>column1</th>
<th>buttons</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="manufacturer in data">
<td>{{manufacturer.name}}</td>
<td>
<button class="btn btn-mini" ng-class="{'btn-success': manufacturer.display, 'btn-danger': !manufacturer.display}" ng-click="change_display($index)"><i class="icon-white" ng-class="{'icon-ok': manufacturer.display==true, 'icon-remove': manufacturer.display==false}"></i></button>
<button class="btn btn-mini" ng-class="{true:'btn-success', false:'btn-danger'}[manufacturer.display_appraisal]" ng-click="manufacturer.display_appraisal = !manufacturer.display_appraisal"><i class="icon-white" ng-class="{'icon-ok': manufacturer.display_appraisal==true, 'icon-remove': manufacturer.display_appraisal==false}"></i></button></td>
</tr>
</tbody>
</table>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.