2017-02-04 77 views
0

我嘗試了許多不同的事情來試圖讓它起作用。 我已閱讀:Angular ng-click not firing

ng-click not firing in AngularJS while onclick does

AngularJS : ng-click not working

和更大量

HTML:

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

JS:

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

出於某種原因,div完全不會淡出。

+1

只是一個提示,你應該避免DOM操作,而使用AngularJS請參考:http://stackoverflow.com/questions/14994391/thinking-in-angularjs-if-i-have-a-jquery-background 。使用AngularJS時,jQuery不是必需/推薦。 – lin

回答

5

您在controller中指定了您的應用程序名稱。檢查這個。

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>

+0

如果我在jsfiddle上這樣做,它不想工作,雖然它在這個頁面上工作。 https://jsfiddle.net/DrevanTonder/sguy6agL/ – DrevanTonder

0

在我看來,這件事情錯了你的控制器的名字。試試這個:

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

0

你可以使用一個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; 
    }; 
}); 

的角動畫 - 參考LINK

0

指定適當的 「NG-應用」 和「 ng-controller「名稱

HTML ie。

<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>