Nes*_*esh 4 javascript html5 angularjs angularjs-scope
以下是我的代码片段,其中我需要在复选框值的基础上聚焦输入字段,如果我选中复选框,它应该聚焦第二个元素,它不知何故不起作用.让我知道我在Angular中做错了什么.
注意 - 我已经使用指令检查了几个例子,但我想知道我在这里缺少的概念/代码等
<input type="checkbox" ng-model="vm.check" />
<br>
<input type="text" ng-model="vm.input1" />
<input type="text" ng-model="vm.input2" ng-focus="vm.check" />
<input type="text" ng-model="vm.input3" />
Run Code Online (Sandbox Code Playgroud)
Plnkr代码 - http://plnkr.co/edit/q15Jht9AsVj60xrutVfm?p=preview
ngFocus不像你想象的那样工作.下面的表达式ngFocus是在输入焦点上触发的,就是全部.
如果要根据var的状态来关注它,则应使用指令.示例:
myApp.directive('syncFocusWith', function($timeout, $rootScope) {
return {
restrict: 'A',
scope: {
focusValue: "=syncFocusWith"
},
link: function($scope, $element, attrs) {
$scope.$watch("focusValue", function(currentValue, previousValue) {
if (currentValue === true && !previousValue) {
$element[0].focus();
} else if (currentValue === false && previousValue) {
$element[0].blur();
}
})
}
}
});
Run Code Online (Sandbox Code Playgroud)
并更新您的html以使用此指令:
<input type="text" ng-model="vm.input2" sync-focus-with="vm.check"/>
Run Code Online (Sandbox Code Playgroud)
从Emberex.com获得Alex分享此片段的信用.
| 归档时间: |
|
| 查看次数: |
5567 次 |
| 最近记录: |