javascript - Ignore directive on page load -
i have setup below directive give focus 'newest' input field added page. works other 1 problem. on page load last input on page selected. want directive ignored on page load can instead set focus on button #new-query
id. there anyway tell directive begin watching scope after user interacts page?
'use strict'; angular.module('app') .directive('setfocus', setfocus); function setfocus($timeout, $parse) { return { link: function(scope, element, attrs) { var model = $parse(attrs.setfocus); scope.$watch(model, function(value) { if (value) { $timeout(function() { element[0].focus(); }); } }); } }; }
an example of how use directive:
<div ng-show="parameter.repeatable" ng-repeat="index in parameter.values track $index"> <input name="parameter" type="text" ng-model="parameter.values[$index]" set-focus="$last"> ...
as inputs added page through user actions focus jumps newest input. problem on page load action not desired, after user begins interacting input fields.
here's workaround might suitable
in controller:
$scope.page ={first_load: true};
directive:
function setfocus($timeout, $parse) { return { link: function(scope, element, attrs) { if (!scope.page.first_load && $last) { $timeout(function() { element[0].focus(); }); } // last item of first rendering of `ng-repeat` update controller if(scope.page.first_load && $last){ scope.page.first_load = false; } }); } }; }
Comments
Post a Comment