jQuery在我的Angular.js网站上无法正常工作

时间:2015-12-10 05:56:17

标签: javascript jquery angularjs

我正在努力让Masonry.js在我的网站上工作,虽然我已经成功了,但这是一个超级hacky解决方案,我不知道为什么它可行(并且它不能正常工作)。

我的主要两个问题是:
    jQuery无法正常工作     它只适用于setTimeout(否则元素堆叠在一起,因为图像在加载之前没有高度)

这是我的代码:(或查看github

masonryStyle.js(这个文件真的很奇怪。只有那个确切的函数在那里才能运行jQuery。删除任何一块并且它会中断)

setTimeout(function() {
var elem = document.querySelector('.grid');
var msnry = new Masonry( elem, {
    itemSelector: '.grid-item'
});
}, 500);

$('.grid').masonry({
columnWidth: '.grid-sizer',
itemSelector: '.grid-item',
percentPosition: true
});

home.html(需要砌筑的内容

<div class = "searchBar">
<form ng-submit = "searchAuthor()" class = "searchSection">
    <input type = "text" class = "searchInput" ng-model = "byAuthor" placeholder = "Search Authors">
    <input type = "submit" class = "searchSubmit" value = "Search">
</form>
<div class = "spacer"></div>
<form ng-submit = "searchTag()" class = "searchSection">
    <input type = "text" class = "searchInput" ng-model = "byTag" placeholder = "Search Tags">
    <input type = "submit" class = "searchSubmit" value = "Search">
</form>
</div>

<div class = "grid">
<div class = "grid-item" ng-repeat = "image in images">
    <img ng-src = "{{image.link}}" class = "image">
    <div class = "imageTitle">{{image.title}}</div>
    <div class = "imageAuthor">Posted by: {{image.author}}</div>
    <div class = "imageUpvote"><a href ng-click = "upvote(image)"><i class="fa fa-thumbs-o-up"></i></a> {{image.upvotes}}</div>
    <div class = "imageTags" ng-repeat = "tag in image.tags">{{tag}}&nbsp;     </div>
</div>
</div>

index.ejs

<!DOCTYPE html>
<html>
<head>
    <title>Pin Viewer</title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
    <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css'>
    <link href='https://fonts.googleapis.com/css?family=Quicksand:400,300' rel='stylesheet' type='text/css'>
</head>
<body ng-app = "app" ng-controller = "MainCtrl">
    <div class = "pageWrapper">
        <div class = "mainPage">
            <div class = "navBar">
                <a href = "#/profile"><div class = "appName">{{user.username}}</div></a>
                <a href = "/login/twitter/"><div class = "navButton" ng-hide = "user.username">Log In</div></a>
                <a href ng-click = "logout()"><div class = "navButton" ng-show = "user.username">Log Out</div></a>
            </div>

            <div class = "pageTitle"><a href = "#/">Pin Viewer</a></div>

            <div class = "postImage">
                <button class = "postImageButton" ng-click = "postImage()">Post Image</button>
            </div>
            <div class = "error">{{error}}</div>
        </div>

        <div class = "pageContent">

        <ui-view></ui-view>

        </div>
    </div>
    <script src="javascripts/jquery-1.11.3.js"></script>
    <script src='javascripts/masonry.pkgd.js'></script>
    <script src='javascripts/angular.min.js'></script>
    <script src='javascripts/angular-ui-router.js'></script>
    <script src="javascripts/angularApp.js"></script> 
</body>
</html>

1 个答案:

答案 0 :(得分:0)

尝试这种有角度的masonary版本:https://github.com/passy/angular-masonry
一些jQuery起源在Angular.js应用程序中出现问题,否则如果你要使用jQuery,你必须在每次路由更改时加载所有jQuery函数

例如:

$rootScope.$on('$viewContentLoaded', function(event) {
      $('#side-menu').metisMenu();
  });`

你必须这样做。