【发布时间】:2017-01-02 17:01:21
【问题描述】:
在这种情况下帮助我,在 ng-repeat 中有按钮,单击按钮时启用文本框,启用文本框我希望在其中获得焦点。也将焦点设置到文本的末尾。
var app = angular.module('app', []);
app.controller('demoController', function($scope, $timeout) {
$scope.testFocus = [{'id': 0, 'isShow': false}, {'id': 1,'isShow': false}];
$scope.buttonClicked = function(f, $index) {
angular.forEach($scope.testFocus, function(value, key) {
$scope.testFocus[key].isShow = (value.id == f.id ? true : false );
});
$timeout(function () {
$('.input-'+ $index).focus();
});
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html ng-app="app">
<head>
<script data-require="angular.js@1.5.5" data-semver="1.5.5" src="https://code.angularjs.org/1.5.5/angular.min.js"></script>
<script data-require="jquery@2.1.1" data-semver="2.1.1" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body ng-controller="demoController">
<div ng-repeat="f in testFocus">
<button class="btn btn-sm chat-option chat-option-add" ng-click="buttonClicked(f, $index)" style="padding: 0px;border-radius: 50%;">button</button>
<input ng-if="f.isShow" value="hello" type="text" class="input-{{$index}}" style="color:black;" />
</div>
</body>
</html>
【问题讨论】:
-
我的 ng-repeat 问题
-
我更新了我的答案:)