用户滚动时的AngularJS切换路径(AngularJS switch path when user scroll)

编程入门 行业动态 更新时间:2024-10-11 19:15:39
用户滚动时的AngularJS切换路径(AngularJS switch path when user scroll)

我试图检测用户何时滚动视图然后我想切换视图。

我在用户滚动时使用指令进行检测:

.directive("scroll", function ($window) { return function(scope, element, attrs) { angular.element($window).bind("scroll", function() { if (this.pageYOffset >= 1) { scope.boolChangeClass = true; console.log('Scrolled down'); } else { scope.boolChangeClass = false; console.log('Scroll up'); } scope.$apply(); }); };

使用ng-controller在div上调用该指令

<div scroll ng-controller="MyCtrl1">

我在Controller 1的视图中获得了boolChangeClass

<span>{{boolChangeClass}}</span>

并将结果记录在我的控制器中

myApp.controller('MyCtrl1', ['$scope', '$http', function($scope, http) { console.log($scope.boolChangeClass); }]);

如何在我的控制器中检测到boolChangeClass已更改,然后我想转到controller2?

I try to detect when the user scroll the view and then I would like to switch view.

I'm using a directive for detect when the user scroll :

.directive("scroll", function ($window) { return function(scope, element, attrs) { angular.element($window).bind("scroll", function() { if (this.pageYOffset >= 1) { scope.boolChangeClass = true; console.log('Scrolled down'); } else { scope.boolChangeClass = false; console.log('Scroll up'); } scope.$apply(); }); };

The directive is called on the div with the ng-controller

<div scroll ng-controller="MyCtrl1">

I get the boolChangeClass in the view of Controller 1

<span>{{boolChangeClass}}</span>

And log the result in my controller

myApp.controller('MyCtrl1', ['$scope', '$http', function($scope, http) { console.log($scope.boolChangeClass); }]);

How can I detect in my controller that boolChangeClass have changed and then I would like to go to controller2 ?

最满意答案

使用$watch来检测范围变量的任何变化。

myApp.controller('MyCtrl1', ['$scope', '$http', function($scope, http) { console.log($scope.boolChangeClass); $scope.$watch(function(){ return $scope.boolChangeClass; }. function() { console.log($scope.boolChangeClass); // will be called when the values gets changed }) }]);

Use $watch to detect any change in the scope variable.

myApp.controller('MyCtrl1', ['$scope', '$http', function($scope, http) { console.log($scope.boolChangeClass); $scope.$watch(function(){ return $scope.boolChangeClass; }. function() { console.log($scope.boolChangeClass); // will be called when the values gets changed }) }]);

更多推荐

本文发布于:2023-08-07 04:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1460680.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   用户   AngularJS   switch   user

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!