2017-09-14 76 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

感谢您的帮助!这对我有效。 –