繁体   English   中英

ng-click事件不起作用

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM