There seems to be a glitch with jQuery on my Angular.js website

I'm trying to implement Masonry.js on my website, and although I've managed to make it work, the solution feels like a messy workaround and I can't quite figure out why it's functioning (and not functioning well).

The primary issues I'm facing are:
- jQuery behaving strangely
- Needing to use a setTimeout function to prevent elements from overlapping before images load

Below is the code snippet: (or visit github )

masonryStyle.js (this script seems convoluted. jQuery only works when certain functions are included. Removing any part causes it to fail)

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 (content that requires Masonry layout)

<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>

Answer №1

Check out this angularized adaptation of masonry: https://github.com/passy/angular-masonry
Some compatibility issues arise when using jQuery in an Angular.js application, which means that all jQuery functions need to be reloaded on every route change if you choose to go with jQuery.

Here is an example:

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

You will need to implement something similar to the above approach for your app.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Ajax response fails to update my viewmodel

I have a value and a list that I need to update from within an Ajax callback. After retrieving a fresh value using .get(), I try to assign it to my view model's property, but the UI does not refresh properly. Below is the code snippet: function Searc ...

Fetch additional information via Ajax request when button is clicked within MVC 4

I am building a feature to display 6 records in an HTML table with a button labeled Load More. The goal is for each click of the button to retrieve another set of 6 records. Here's what I have tried so far: Within the Controller [HttpGet] private Js ...

Encountered an issue while running the npm start command in AngularJS

As a beginner in angular js, I recently started learning through this tutorial. However, when I attempt to run the development web server using npm start, I encounter the following error: > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" ...

Unleashing the Potential: Integrating a Scanner Device with

Currently, I'm facing the challenge of integrating a Scanner device with my Angular application. On one of the pages dedicated to scanning, users are able to view an alert indicating the connection status of the Scanner as well as any scanned document ...

.NET and Azure integration for asynchronous HTTP requests

My Ajax function is running smoothly on my local machine when the site is accessed locally. However, an issue arises when deploying it to Azure - if one of the parameters contains a line break, I get an error message: "The specified URL cannot be found", i ...

What's causing nested promises to fail in my code?

Despite never having attempted nested promises before, I decided to give it a try based on what I've read and learned online. However, when implementing nested promises in my code, things didn't go as expected: APIService.getData(Config + header ...

Adjust the size of an HTML image for printing using a JavaScript window.print() function

On my website, I have a print option set up where specific elements are hidden using @media. How can I adjust the size of an image within the @media query when my JavaScript print function is triggered? I am able to modify a regular div element easily, but ...

Animating text with a shake effect using JQuery

I am attempting to create a shake effect on my text input field when validation fails. While I have achieved the shake effect, I am unsure how to customize the default values for direction, distance, and times. Additionally, I believe there is an error i ...

Is it possible to use a jQuery pop-up div box to make edits to a selected

I have a situation where I need to update the rating of LI elements with span text that initially reads "unrated". My goal is to create a functionality where users can press a button, trigger a popup div where they can select a rating from multiple radio b ...

I am wondering if it is feasible for a POST route to invoke another POST route and retrieve the response ('res') from the second POST in Express using Node.js

Currently, I have a POST route that triggers a function: router.route('/generateSeed').post(function(req,res){ generate_seed(res) }); UPDATE: Here is the genrate_seed() function function generate_seed(res) { var new_seed = lightwallet. ...

Make an axios request multiple times equal to the number of items in the previous response

In my project, I am using the axios library to convert addresses into their respective coordinates. First, I fetch a list of addresses from an API. Next, I take the RESPONSE object and use Google API to convert each address to coordinates. Finally, I wan ...

Having difficulty creating a shadow beneath a canvas displaying Vega charts

I'm looking to create a floating effect for my chart by adding shadows to the canvas element that holds it. Despite trying various methods, I can't seem to get the shadow effect to work. Here is the code snippet I have for adding shadows: <sc ...

Babylon entities failing to run

Encountering a strange issue in my current project. I am working on a react application that incorporates Babylonjs for 3D rendering. While I am able to successfully load objects into the scene, I am facing a problem when attempting to create a PBR Materia ...

I must add and display a tab for permissions

I am currently using the material UI tab for my project. My goal is to display the tab only when the permission is set to true. I have successfully achieved this functionality, but the issue arises when the permission is false. It results in an error that ...

Loop through JSON data using jQuery for parsing

Here is the code snippet I have included below, which is a part of a larger code base: <html> <head> <style> #results{margin-top:100px; width:600px; border:1px solid #000000; background-color:#CCCCCC; min-height:200px;} </style> & ...

Can you explain the meaning of arguments[0] and arguments[1] in relation to the executeScript method within the JavascriptExecutor interface in Selenium WebDriver?

When utilizing the executeScript() method from the JavascriptExecutor interface in Selenium WebDriver, what do arguments[0] and arguments[1] signify? Additionally, what is the function of arguments[0] in the following code snippet. javaScriptExecutor.ex ...

Navigate through stunning visuals using Bokeh Slider with Python callback functionality

After being inspired by this particular example from the Bokeh gallery, I decided to try implementing a slider to navigate through a vast amount of collected data, essentially creating a time-lapse of biological data. Instead of opting for a custom JavaS ...

Leveraging Vue.js's computed properties to access and manipulate elements within an

I have a simple template that displays text from a wysiwyg editor using two-way data binding, as shown below: <template> <div> <quill-editor v-model="debounceText" :options="editorOptionProTemplate" > </qu ...

The async and await functions do not necessarily wait for one another

I am working with Typescript and have the following code: import sql = require("mssql"); const config: sql.config = {.... } const connect = async() => { return new Promise((resolve, reject) => { new sql.ConnectionPool(config).connect((e ...

Discover properties of a TypeScript class with an existing object

I am currently working on a project where I need to extract all the properties of a class from an object that is created as an instance of this class. My goal is to create a versatile admin page that can be used for any entity that is associated with it. ...