简体   繁体   English

角ng-click不触发

[英]Angular ng-click not firing

I have tried many different things to try to get this to work. 我尝试了许多不同的方法来尝试使它起作用。 I have read: 我读过了:

ng-click not firing in AngularJS while onclick does ng-click不会在AngularJS中触发,而onclick会触发

AngularJS : ng-click not working AngularJS:ng-click不起作用

and a lot more 还有更多

Html: HTML:

<div ng-controller="testApp">
  <div id="bla">
    <button ng-click="obey('bla')">Close</button>
    <h4>Bla bla bla</h4>
  </div>
</div>

JS: JS:

var testApp = angular.module('testApp', []);
testApp.controller('testController', function($scope) {
  $scope.obey = function test(id) {
    $("#" + id).fadeOut("slow", function() {
      this.remove()
    });
  };
});

For some reason the div does not fade out at all. 由于某种原因,div根本不会消失。

You specified your app name in controller . 您在controller指定了应用程序名称。 check this. 检查一下。

 var testApp = angular.module('testApp', []); testApp.controller('testController', function($scope) { $scope.obey = function test(id) { $scope.hide= !$scope.hide; }; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="testApp" ng-controller="testController"> <div id="bla"> <button ng-click="obey('bla')">Close</button> <h4 ng-hide="hide">Bla bla bla</h4> </div> </div> 

Seems to me it's something wrong with name of your controller. 在我看来,您的控制器名称有误。 Try this: 尝试这个:

<div ng-app="testApp" ng-controller="testController">
  <div id="bla">
    <button ng-click="obey('bla')">Close</button>
    <h4>Bla bla bla</h4>
  </div>

you can use a ng-show or ng-hide in this case 在这种情况下,您可以使用ng-showng-hide

<div ng-controller="testApp">
  <div id="bla">
    <button ng-click="obey()">Close</button>
    <h4  ng-show="viewDiv">Bla bla bla</h4>
  </div>
</div>

var testApp = angular.module('testApp', []);
testApp.controller('testController', function($scope) {
 $scope.viewDiv = true;
  $scope.obey = function test(id) {
   $scope.viewDiv = !$scope.viewDiv;
  };
});

for angular animation - refer LINK 有关角度动画的信息,请参阅LINK

specified proper "ng-app" and "ng-controller" name 指定正确的“ ng-app”和“ ng-controller”名称

HTML ie. HTML即

<body ng-app="testApp">
    <div ng-controller="testController">
      <div id="bla">
        <button ng-click="obey('bla')">Close</button>
        <h4>Bla bla bla</h4>
      </div>
    </div>
<body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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