Javascript 焦点文本区域在ng上单击
我想单击Javascript 焦点文本区域在ng上单击,javascript,html,angularjs,angularjs-ng-repeat,Javascript,Html,Angularjs,Angularjs Ng Repeat,我想单击ng repeat中的一个按钮,然后关注循环中特定项目的textarea HTML <div ng-repeat="item in items"> <textarea focus-when="showTextarea">{{ item }}</textarea> <a ng-click="trigger()" href="#">Focus</a> </div> <div ng-controller="
ng repeat
中的一个按钮,然后关注循环中特定项目的textarea
HTML
<div ng-repeat="item in items">
<textarea focus-when="showTextarea">{{ item }}</textarea>
<a ng-click="trigger()" href="#">Focus</a>
</div>
<div ng-controller="MyCtrl">
<div ng-repeat="item in items">
<button class="btn" ng-click="showForm=true; focusInput=true">show form and focus input #{{ item }}</button>
<div ng-show="showForm">
<input type="text" focus-me="focusInput">
<button class="btn" ng-click="showForm=false">hide form</button>
</div>
</div>
</div>
代码:
我有指令focusWhen
,它观察了showTextarea
范围变量。当ng单击
trigger时,它会将此showTextarea
更改为true,并应聚焦textarea
然而,我的代码不起作用,它有错误
错误:[$injector:unpr]未知提供程序:$scopeProvider更改
app.directive('focusWhen', function($scope) {
到
是的,我犯了错误,但我只是把问题分开,仍然在上面的#2
上寻找答案。
var app = angular.module('plunker', []);
var MyCtrl = function ($scope, $timeout) {
$scope.items = [1,2,3];
};
app.directive('focusMe', function($timeout) {
return {
scope: { trigger: '=focusMe' },
link: function(scope, element) {
scope.$watch('trigger', function(value) {
if(value === true) {
//console.log('trigger',value);
//$timeout(function() {
element[0].focus();
scope.trigger = false;
//});
}
});
}
};
});
app.directive('focusWhen', function($scope) {
app.directive('focusWhen', function() {