Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 添加ng模糊时,角度ng单击未激发_Javascript_Angularjs_Angularjs Ng Click - Fatal编程技术网

Javascript 添加ng模糊时,角度ng单击未激发

Javascript 添加ng模糊时,角度ng单击未激发,javascript,angularjs,angularjs-ng-click,Javascript,Angularjs,Angularjs Ng Click,我尝试组合ng click和ng blur,但这会导致错误,ng click未被激发 我有输入,下面是我在输入集中时显示的选项列表 <div ng-app="myApp" ng-controller="MyCtrl"> <input type="text" ng-model="value" ng-focus="onFocus()" ng-blur="onBlur()" > <ul ng-show="visible">

我尝试组合ng click和ng blur,但这会导致错误,ng click未被激发

我有输入,下面是我在输入集中时显示的选项列表

<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>
以下是完整样本: 这是因为模糊事件发生在单击事件之前,这意味着单击发生时列表项已经隐藏

您可以延迟列表的隐藏,例如使用angular的$timout服务:

请参阅更新的示例:

这是因为模糊事件发生在单击事件之前,这意味着单击发生时列表项已经隐藏

您可以延迟列表的隐藏,例如使用angular的$timout服务:

请参阅更新的示例:

问题在于,在ng click启动之前,正在处理输入的onBlur。。。您可以通过将ng click更改为ng mouseover来测试代码。问题是,在ng click触发之前,输入的onBlur已被处理。。。您可以通过将ng click更改为ng mouseover来测试代码,因为在执行ng click之前会调用ng blur函数。因此,您可以使用$timeout设置$scope.visible=false

您可以设置$scope.visible=false,而不是使用ng blure;在$scope.setValue函数中。比如:

在控制器中:

$scope.setValue = function(value) { 
    $scope.value = value; 
    $scope.visible = false;
};
和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>
因为在执行ng click之前调用了ng blur函数。因此,您可以使用$timeout设置$scope.visible=false

您可以设置$scope.visible=false,而不是使用ng blure;在$scope.setValue函数中。比如:

在控制器中:

$scope.setValue = function(value) { 
    $scope.value = value; 
    $scope.visible = false;
};
和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>
嗯,鼠标悬停只是验证问题原因的一种方式。。。有几种方法可以解决这个问题。。。一种方法是使用如下所示的延迟。。。另一种方法是为整个页面创建一个点击处理程序,并使用该处理程序而不是将鼠标悬停作为验证问题原因的一种方法。。。有几种方法可以解决这个问题。。。一种方法是使用如下所示的延迟。。。另一种方法是为整个页面创建一个点击处理程序,并使用它而不是onBlur
<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>