我在每個重複項目中都有輸入框的ng-repeat。我已將自動對焦設置爲輸入。根據條件返回true的HTML屬性
<input type="text" autofocus />
所以自動對焦將應用於所有項目。但是,iOS中存在一個最後一個輸入框自動聚焦而不是第一個輸入框的錯誤。
我需要將其設置爲根據條件在返回true:
<input autofocus="{{$first ? 'true' : 'false' }}" >
不知道如何用角度做到這一點?
我在每個重複項目中都有輸入框的ng-repeat。我已將自動對焦設置爲輸入。根據條件返回true的HTML屬性
<input type="text" autofocus />
所以自動對焦將應用於所有項目。但是,iOS中存在一個最後一個輸入框自動聚焦而不是第一個輸入框的錯誤。
我需要將其設置爲根據條件在返回true:
<input autofocus="{{$first ? 'true' : 'false' }}" >
不知道如何用角度做到這一點?
使用自定義指令到焦點的元素
指令
app.directive('autofocus', ['$timeout', function($timeout) {
return {
restrict: 'A',
link : function(scope, element,attrs) {
$timeout(function() {
if(attrs.autofocus==='true'){
element[0].focus();
}
});
}
}
}]);
控制器
app.controller('MainCtrl', function($scope) {
$scope.names = ['first','second','third','fourth','fifth'];
});
HTML
<div ng-repeat="name in names">
<input type="text" autofocus="{{$index==0}}" ng-model="name" />
</div>
Demo plunker link
'$ first? 'true':'false'可以縮寫爲'$ first';不要使用「true」和「false」的字符串表示。 – msanford