简体   繁体   English

添加ng-blur时不触发角ng-click

[英]Angular ng-click not fired when ng-blur added

I try to combine ng-click and ng-blur but it causes error, ng-click is not fired. 我尝试将ng-click和ng-blur结合使用,但会导致错误,不会触发ng-click。

I have input and below I have list of options which I show when input is focused. 我有输入,下面有我将输入聚焦时显示的选项的列表。

<div ng-app="myApp" ng-controller="MyCtrl">
  <input type="text"
    ng-model="value"
    ng-focus="onFocus()"
    ng-blur="onBlur()"
  >
  <ul ng-show="visible">
    <li ng-repeat="i in items" ng-click="setValue(i)">{{i}}</li>
  </ul>
</div>

On items on list I defined ng-click. 在清单上的项目中,我定义了ng-click。 It set new value in model. 它在模型中设置了新值。 It works, but when I add function which hide options on input blur new value is not updated. 它有效,但是当我添加隐藏输入模糊选项的功能时,不会更新新值。

Here is my js code: 这是我的js代码:

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

function MyCtrl($scope) {
    $scope.visible = false;
    $scope.items = ["Foo", "Bar", "Baz"];
    $scope.onFocus = function() {$scope.visible = true;};
    $scope.onBlur = function() {$scope.visible = false;};
    $scope.setValue = function(value) { $scope.value = value; };
}

myApp.controller('MyCtrl', MyCtrl)

Here is full sample: https://jsfiddle.net/uqs19rea/ 这是完整的示例: https : //jsfiddle.net/uqs19rea/

because of ng-blur function call before execute ng-click . 由于在执行ng-click之前调用ng-blur函数。 so you can use $timeout to set $scope.visible = false; 因此您可以使用$timeout设置$scope.visible = false;

$timeout(function(){ // ensure that $timeout injected in your controller
   $scope.visible = false;
},100);

OR 要么

instead of use ng-blure you can set $scope.visible = false; 可以使用$scope.visible = false;来代替使用ng-blure $scope.visible = false; in $scope.setValue function. $scope.setValue函数中。 like: 喜欢:

in controller: 在控制器中:

$scope.setValue = function(value) { 
    $scope.value = value; 
    $scope.visible = false;
};

and html: 和html:

<div ng-app="myApp" ng-controller="MyCtrl">
    <input type="text" ng-model="value" ng-focus="onFocus()">
    <ul ng-show="visible">
      <li ng-repeat="i in items" ng-click="setValue(i)">{{i}}</li>
    </ul>
 </div>

This is because the blur event happens before the click event, which means the list items are already hidden when the click happens. 这是因为blur事件发生在click事件之前,这意味着单击发生时列表项已经被隐藏。

You can delay the hiding of the list, eg by using angular's $timout service: 您可以延迟列表的隐藏,例如通过使用angular的$timout服务:

function MyCtrl($scope, $timeout) {
    $scope.visible = false;
    $scope.items = ["Foo", "Bar", "Baz"];
    $scope.onFocus = function() {$scope.visible = true;};
    $scope.onBlur = function() {
        $timeout(function() {$scope.visible = false;}, 200);
    };
    $scope.setValue = function(value) { $scope.value = value; };
}

See the uppdated sample: https://jsfiddle.net/uqs19rea/1/ 请参阅更新的示例: https ://jsfiddle.net/uqs19rea/1/

问题是您的输入的onBlurng-click()触发之前onBlur得到处理...您可以通过将ng-click更改为ng-mouseover来测试代码

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

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