简体   繁体   English

index.html中的jquery可以访问链接控制器中的变量吗?

[英]Can jquery in index.html access variables in linked controller?

I'm using jQuery to make a dropdown nav that changes its css value based on whether a user is logged in to the app or not. 我正在使用jQuery创建一个下拉导航,它根据用户是否登录到应用程序来更改其css值。 Links visible on the nav change based on whether or not the loginStatus var is true or not in the app's controller: 导航中可见的链接会根据应用程序控制器中的loginStatus var是否为true进行更改:

index.html: index.html:

<ul id="links">
    <li ng-if="loginStatus !== true">
        <div>
            <a class="active" ui-sref="home" ui-sref-active="active">Home</a>
        </div>
    </li>
    <li>
        <div>
            <a class="active" ui-sref="cardSearch" ui-sref-active="active">Card Search</a>
        </div>
    </li>
    <li ng-if="loginStatus == true">
        <div>
            <a class="active" ui-sref="deckBuilder" ui-sref-active="active">Deck Builder</a>
        </div>
    </li>
    <li ng-if="loginStatus == true">
        <div>
            <a class="active" ui-sref="deckCollection" ui-sref-active="active">Deck Collection</a>
        </div>
    </li>
    <li ng-if="loginStatus == true">
        <div>
            <a class="active" ng-click="logout()" ui-sref-active="active">Log Out</a>
        </div>
    </li>
</ul>

controller.js controller.js

angular.module('mainApp').controller('mainController', function($scope, mainService, $state) {

    $scope.loginStatus = false;

    $scope.logout = function() {
        sessionStorage.removeItem('user');
        $scope.loginStatus = false;
        console.log($scope.loginStatus)
        $state.go('home');

    }

    $scope.loginStatus = sessionStorage.getItem('user') ? true : false;

    $scope.$on('reloadController', function() {
        console.log('Something is happening')
        $scope.loginStatus = sessionStorage.getItem('user') ? true : false;
    })

});

how can I get the jQuery in my index to recognize the variable in my controller so that its methods can work? 如何在索引中获取jQuery以识别控制器中的变量,以便其方法可以工作?

<script>
    $(document).ready(function() {

        var dropDownDisplayed = false;

        // jQuery methods go here...
        $(".fa-bars").click(function() {
            console.log('runnin ', dropDownDisplayed)

            if (dropDownDisplayed == false) {
                $("#links").css("display", "flex");
                dropDownDisplayed = true;
            } else if (dropDownDisplayed == true) {
                $("#links").css("display", "none");
                dropDownDisplayed = false;
            }
        });

        $(window).resize(function() {
            console.log('window.innerwidth', window.innerWidth)
            if (window.innerWidth > 1024 || dropDownDisplayed == true) {
                $("#links").css("display", "flex");
            } else {
                $("#links").css("display", "none");
            }
        })

        // dropdown menu while loggedout
        $(window).on("resize", function() {
            if (loginStatus !== true && $(window).width() <= 700) {
                $("#links").css("top", "140px");
            }
        })

        // dropdown menu while loggedin
        $(window).on("resize", function() {
            if (loginStatus == true && $(window).width() <= 1024) {
                $("#links").css("top", "500px");
            }
        })

    });
</script>

No. Your jQuery is not going to be able to access scope variables on your controller. 不。您的jQuery将无法访问控制器上的作用域变量。 Not directly... But at worst, there's always the window object which you could modify the Angular code to store it (or copy it) there. 不是直接...但是最糟糕的是,总是有window对象,您可以修改Angular代码以将其存储(或复制)到那里。

As a side... Angular is going to add/remove things to the DOM after document ready. 顺便说一句...在准备好文档后,Angular将向DOM添加/删除内容。 So only bits and pieces of your jQuery are going to work depending on how you write them. 因此,取决于您的编写方式,只有jQuery的各个部分才能起作用。 However, you should find that your handlers on the window events will trigger. 但是,您应该发现window事件的处理程序将触发。

Assuming that your .fa-bars elements are now being rendered by Angular, you'll find that this one will not work: 假设您的.fa-bars元素现在由Angular呈现,您会发现这一元素将不起作用:

$(".fa-bars").click(function(){ });

Because the .fa-bars are not necessarily there at document ready, and potentially Angular has done away with them in response to certain actions. 因为.fa-bars不一定在准备好文档时就存在,并且Angular可能已.fa-bars它们以响应某些操作。

You can fix that a little by catching the bubbled click event: 您可以通过捕获冒泡的click事件来解决此问题:

$(document).on("click", ".fa-bars", function() {});

All that being said... If this is all the jQuery code that you have, I would just convert it to Angular and throw the old stuff away. 话虽这么说...如果这就是您拥有的所有jQuery代码,我将其转换为Angular并丢弃旧内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM