2017-08-02 46 views
0

我覺得這非常的bizzare,NG-點擊不工作這個元素上,NG-點擊不工作的李

<div class="row"> 
    <div class="col-md-12"> 
     <div class="form-group label-floating is-empty"> 
      <label class="control-label">category</label> 
       <input type="text" class="form-control" onkeydown="return false;" ng-focus="autocomplete != autocomplete" ng-blur="autocomplete != autocomplete"> 
       <div class="autocomplete-c" ng-show="autocomplete"> 
        <ul> 
         <li ng-repeat="d in categories" ng-click="selectCategory(d)"> 
          <p>{{d.name}}</p> 
          <small>{{d.types}}</small> 
         </li> 
        </ul> 
       </div> 
       <span class="material-input"></span> 
      </div> 
     </div> 
    </div> 

並將此作爲功能,

$scope.selectCategory = function(d){ 
    console.log(d); 
} 

這有沒有事件觸發,我需要幫助。

+2

發表小提琴重現問題。 –

+0

@sabi檢查http://jsfiddle.net/mayankcpdixit/HgDA7/小提琴,它似乎沒有問題與您的代碼 – Rahul

回答

2

我懷疑是用ng-blurng-focus您點擊功能是不是讓你點擊之前註冊的,因爲DIV是越來越隱蔽在名單上。 我的建議是不要使用ng-blurng-focus,而應該點擊輸入。

工作小提琴:http://jsfiddle.net/ADukg/13591/

0

我懷疑問題是在觸發ng-click事件之前隱藏了「.autocomplete-c」div,所以一些更改似乎可以修復它。

<input type="text" class="form-control" onkeydown="return false;" ng-model="categorySelected" ng-focus="autocomplete = true"> 
    <div class="autocomplete-c" ng-show="autocomplete"> 
     <ul> 
      <li ng-repeat="d in categories" ng-click="selectCategory(d)"> 
       <p>{{d.name}}</p> 
       <small>{{d.types}}</small> 
      </li> 
     </ul> 
    </div> 

與控制器看起來像這樣

$scope.selectCategory = function(d){ 
    console.log(d); 
    $scope.valued = "is-focused"; 
    $scope.categorySelected = d.name+", "+d.types; 
    $scope.categoryID = d.categoryID; 
    $scope.autocomplete = false; 
} 
0

使用下面的代碼,這按預期方式工作(Working Plnkr)。也許有這樣的概述會有所幫助。

使用所提供的代碼,不清楚您的環境是如何設置的。假設沒有錯誤(你正在查找控制檯),我認爲你可能有一個範圍問題,其中selectCategory()不存在。

快速檢查可以簡單地插入一個原始值,如設置$ scope.whereami =「SomeController」;

然後,在您嘗試在ng-repeat中調用selectCategory()的同一級別,添加{{whereami}}

我發佈了一個問題,我可能也會影響你,這是ng-repeat創建它自己的子範圍的事實。根據你的情況,這也可能發揮作用:ng-model not updating AngularJS

應用程序/控制器

var app = angular.module('app', []); 

app.controller('MainCtrl', ['$scope', function ($scope) { 
    $scope.categories = [ 
     {'name': 'cat1', 'types': ['A-Type', 'B-Type'] }, 
     {'name': 'cat2', 'types': ['C-Type', 'D-Type'] } 
    ]; 

    $scope.selected = {}; 

    $scope.selectCategory = function(d){ 
     console.log(d); 
     $scope.selected = d; 
    } 

}]); 

HTML

<!doctype html> 
<html ng-app="app"> 
    <head> 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script> 
    </head> 
    <body> 

    <div ng-controller="MainCtrl"> 

     <ul> 
     <li ng-repeat="d in categories" ng-click="selectCategory(d)"> 
      <p>{{d.name}}</p> 
      <small>{{d.types}}</small> 
     </li> 
     </ul> 

     <div> 
     <h3>Clicked</h3> 
     Name: {{ selected.name }} <br /> 
     Type: {{ selected.types }} 
     </div> 
    </div> 

    <script src="app.js"></script> 
    </body> 
</html>