2017-09-14 78 views
0

Angular JS新手在這裏。我正在嘗試對我沒有構建的網站進行基本更改。ng-click event not working

我在名爲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"> 

任何想法?

+0

另一個角新手在這裏。你確定你的改變方法錯了嗎?也許更新功能有問題嗎?你有沒有試圖用制動點來調試點擊處理程序?在chrome調試器中,您可以設置制動點,並且可以按照應用程序的流程進行操作。 –

+0

謝謝你的建議。 :) –

回答

0

這是一個plknr有兩種方式來做你所需要的。所有的

Sample

首先我創建了一個數據假設匹配你的,因爲你沒有提供的加工廠商陣列。所以。

我把兩種方法你可以改變按鈕的類。

第一種是使用納克級這樣{'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屬性的功能。

查看樣本以獲取更多詳細信息。

SCRIPT

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="[email protected]" 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> 
+0

感謝您的幫助!這對我有效。 –