I have used ng-click
on div and it works as expected, but when I have used ng-blur
on some other input, the ng-click
on the div stops working.
我已经使用了ng-click on div并且它按预期工作,但是当我在其他输入上使用ng-blur时,div上的ng-click停止工作。
Working code [addItem(item) is being called on click]
工作代码[点击时调用addItem(item)]
<div ng-controller="TestController">
<input type="text" ng-focus="show=true">
<div ng-show="show" class="choosecont">
Choose from selected
<div ng-repeat="item in allItems" ng-click="addItem(item)" class="choose">{{item}}</div>
</div>
<div class="chosencont">
Following are selected
<div ng-repeat="item in selectedItems" class="chosen">{{item}}</div>
</div>
</div>
Broken code [addItem(item) not being called]
破解的代码[addItem(item)未被调用]
<div ng-controller="TestController">
<input type="text" ng-focus="show=true" ng-blur="show=false">
<div ng-show="show" class="choosecont">
Choose from selected
<div ng-repeat="item in allItems" ng-click="addItem(item)" class="choose">{{item}}</div>
</div>
<div class="chosencont">
Following are selected
<div ng-repeat="item in selectedItems" class="chosen">{{item}}</div>
</div>
</div>
Related JS code
相关的JS代码
angular.module("myApp", [])
.controller("TestController", ["$scope", function($scope) {
$scope.allItems = ["A", "B", "C", "D", "E"];
$scope.selectedItems = [];
$scope.addItem = function(item) {
if ($scope.selectedItems.indexOf(item) == -1)
$scope.selectedItems.push(item);
}
}
]);
Here is plunkr http://plnkr.co/edit/eI5dvczO2E1Cp1SBPgQx?p=preview Click on input which will bring dropdown. Clicking on dropdown adds item to selected list in one case but not in other case.
这是plunkr http://plnkr.co/edit/eI5dvczO2E1Cp1SBPgQx?p=preview点击输入将带来下拉列表。单击下拉列表会在一个案例中将项目添加到选定列表,但在其他情况下则不会。
I have tried to debug. scope is set correctly and it was accessible.
我试过调试。范围设置正确,可以访问。
1 个解决方案
#1
10
The click
event fires after blur
, so the list is being hidden before you are able to click it. The simple solution is to use mousedown
event instead of click
:
点击事件在模糊后触发,因此列表在您单击之前被隐藏。简单的解决方案是使用mousedown事件而不是单击:
ng-mousedown="addItem(item)"
Here is an update to your plunkr: http://plnkr.co/edit/sPGIb1afCayS1UiP73Q0?p=preview
以下是您的plunkr的更新:http://plnkr.co/edit/sPGIb1afCayS1UiP73Q0?p = preview
#1
10
The click
event fires after blur
, so the list is being hidden before you are able to click it. The simple solution is to use mousedown
event instead of click
:
点击事件在模糊后触发,因此列表在您单击之前被隐藏。简单的解决方案是使用mousedown事件而不是单击:
ng-mousedown="addItem(item)"
Here is an update to your plunkr: http://plnkr.co/edit/sPGIb1afCayS1UiP73Q0?p=preview
以下是您的plunkr的更新:http://plnkr.co/edit/sPGIb1afCayS1UiP73Q0?p = preview