简体   繁体   English

AngularJS ng-click不起作用

[英]AngularJS ng-click not working

So i'm taking a stab at learning AngularJS starting this weekend and one thing I can't quite get to work is the ng-click functionality, could anyone offer a suggestion as to why its not working along with any general advice on anything i've done wrong? 所以我在这个周末开始学习AngularJS,而我无法完全使用的一件事就是ng-click功能,有人可以提出建议,为什么它不能与任何有关我的一般建议一起使用做错了吗?

To be clear the dropdown gets filled fine, I want to be able to catch a click for the items on it! 为了清楚起见,下拉列表可以很好地填充,我希望能够点击一下其中的项目!

The HTML: HTML:

<html ng-app="slotDemo">

  <div ng-app ng-controller="gameList" >
        <div class="dropdown">
           <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-expanded="false">
    Please Pick a Game
    <span class="caret"></span>
  </button>

  <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
    <li ng-repeat="slot in slots" role="presentation" ng-click="gameList.fetchGame(slot.id)"><a role="menuitem" href="#">{{slot.id}} {{slot.name}}</a></li>
  </ul>
  </div>

</div>

And the javascript: 和javascript:

var slotDemo = angular.module('slotDemo', [])
        .controller('gameList', gameList1);

function gameList1($scope, $http){
          $scope.slots = [];
          $http({
          method: 'GET',
          url: 'http://localhost/api/machine/list'
          }).success(function(data){
              $scope.slots = data;
          }).error(function(){
              alert("Error");
          });

          $scope.fetchGame = function(id){
              // Fetch the HTML for the slot we just clicked on...
              $scope.machineDetails = [];
              alert("oi");
              $http({
          method: 'GET',
          url: 'http://localhost/api/machine/get/' + id
          }).success(function(data){
                  $scope.machineDetails = data;
          }).error(function(){
              alert("Unable to retrieve slot machine information.");
          });
        };
    }

You need to drop the gameList prefix: 您需要删除 gameList前缀:

ng-click="fetchGame(slot.id)"

Otherwise angular will look for a gameList object(with a fetchGame method) on your scope and you dont have one. 否则,angular会在您的范围内寻找一个gameList对象(带有fetchGame方法),而您没有一个。

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

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