javascript - Scroll to element ID if ng-model is true AngularJS -
i have page display list (result of search input).
i'm using ng-model
capture input text: ng-model="search_text"
.
thing want autoscroll div containing list every time model has something. if empty should nothing, if written in search_text
should trick , auto-scroll down list results.
you achieve simple directive in demo fiddle. uses $anchorscroll()
implement auto-scroll feature.
view
<div ng-controller="myctrl"> <input ng-model="searchtext" type="text"> <div id="list" auto-scroller id-to-scroll-to="'list'" trigger="searchtext"> list </div> </div>
angularjs application / directive
var myapp = angular.module('myapp',[]); myapp.controller('myctrl', function ($scope) {}); myapp.directive('autoscroller', function ($location, $anchorscroll, $timeout) { return { restrit: 'a', scope: { trigger: '=', idtoscrollto: '=' }, link: function (scope, element, attrs) { scope.$watch('trigger', function (newvalue, oldvalue) { if (newvalue && newvalue !== oldvalue) { $timeout(function () { $location.hash(scope.idtoscrollto); $anchorscroll(); }); } }); } } });
Comments
Post a Comment