javascript - 使用 ng-hide/show 和 bool 值时切换 <a> 标记失败

标签 javascript jquery html css angularjs

我有一个向上箭头,应该 ng-hide如果this.showUpArrow等于 false 且 ng-show如果this.showUpArrow等于假。 this.showUpArrow设置为false页面加载时仅切换到 true当页面顶部到达指定的div时的.offsetTop 。我让它识别何时应该将 bool 值转换为 true一旦达到div很好,花花公子。问题是它不是 ng-show荷兰国际集团<a>我希望它在 this.showUpArrow 时显示的标签设置为true 。给出了什么?

这是我的 HTML...

<a href='' id='up-arrow-link' ng-click='landingControl.goToAnchor("wrapper")'>
    <img id='up-arrow' src='../static/images/uparrow.png' ng-hide='!landingControl.showUpArrow' ng-show='landingControl.showUpArrow'>
</a>

这是我的 Angular Controller ...

myModule.controller('landingController', function($location, $anchorScroll, $window){
var _this = this;
this.showUpArrow = false;

    angular.element($window).bind("scroll", function() {
        var elem = angular.element('#mystory')[0].offsetTop
        var topOfScreen = $window.scrollY;
        if (topOfScreen >= elem) {
            _this.showUpArrow = true;

        }
    });
})

值得一提的是landingControl我的一部分ng-hideng-show声明引用了我的 Controller 。我正在使用this方法而不是 $scope方法。

最佳答案

您需要使用 $scope.apply$timeout 调用摘要周期。

$apply() is used to execute an expression in angular from outside of the angular framework. (For example from browser DOM events, setTimeout, XHR or third party libraries). Because we are calling into the angular framework we need to perform proper scope life cycle of exception handling, executing watches.1

这通常在指令中完成。

关于javascript - 使用 ng-hide/show 和 bool 值时切换 <a> 标记失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35820933/

相关文章:

javascript - 单击 "x"关闭图标时,箭头向后切换(0 度)

jquery - 不能将两个 div 并排放置

javascript - Angular 拦截器和 CORS

javascript - jQuery UI 对话框链接

jquery - 单击超链接后显示 jquery 对话框

javascript - DataTables - 按图像的替代字符串对列进行排序

javascript - Safari iOS 上图像 &lt;input&gt; 打开对话框的位置

javascript - 如何在此脚本中添加更多标题?

javascript - Accordion 和标签之间的区别

javascript - 同一分页符上有 2 个脚本