Issue with NG Style not binding to page as expected

I am currently working on creating a web page with two sections that can be horizontally slid back and forth to occupy different widths on the page. My idea was to monitor the width percentage of the draggable bar that separates the two panes/sections, and use ng_style to automatically adjust the widths of the two "panes" based on where the bar is dragged.

This project is in a Rails app with Angular integrated. The Angular part is loading without any errors, and everything else is functioning as expected. However, the ng_style is being loaded from the Angular controller when the page first loads up, but it's not updating when I try to drag the spacer between the two sections.

Below is a simplified version of my HAML (similar to Jade) code:

  #full-page.fluid{ ng_controller: "ExerciseCtrl", ng_mousemove: 'updateSidebarWidth($event)', ng_mouseup: 'untrackMouseMove()', ng_cloak: true }
    .spacer
    .fixed-section-container
      .exercise-show
        %div
          .sidebar-section{ ng_style: '{{ sidebarWidthStyle }}' }
            .sidebar_header  

           .sidebar
              %div{ markdown: @exercise.body }
          .sidebar-toggle{ ng_mousedown: 'trackMouseMove()' }
          .work_area{ ng_style: '{{ workAreaWidthStyle }}' }

Here are the relevant lines in my Angular Controller written in Coffeescript:

  $scope.sidebarWidth = 35
  $scope.trackingMouse = false

  $scope.trackMouseMove = -> $scope.trackingMouse = true
  $scope.untrackMouseMove = -> $scope.trackingMouse = false

  $scope.updateSidebarWidth = (event) -> 
    if $scope.trackingMouse
      pageWidth = $('.emelyn-layout.middle.fluid').width()
      x_percent = (event.pageX * 100) / pageWidth
      x_percent = Math.max( Math.min(100, x_percent), 0 )
      $scope.sidebarWidth = x_percent
      $scope.workAreaWidthStyle = { width: "#{99 - $scope.sidebarWidth}%", marginLeft: "#{$scope.sidebarWidth + 1}%" }
     $scope.sideBarWidthStyle = { width: "#{$scope.sidebarWidth}%" }

In essence, there is a .sidebar-section on the left, followed by a .sidebar-toggle which is meant to be draggable, causing the width styles to change upon successful implementation. Finally, there is the .work-area which adjusts its size along with the .sidebar when the toggle is dragged.

The current issue is that although the initial ng_styles load on page load and visually update when inspecting the page and dragging the toggle bar, these changes do not reflect in the actual style attributes of the elements.

When inspecting the element, the output looks like this:

<div class="work_area" ng_style="{ 'width':'48.142857142857146', 'marginLeft':'51.857142857142854' }">

I referenced this post and attempted wrapping style updates in a $scope.$watch, but unfortunately, it did not alter any behaviors mentioned above. Based on the documentation regarding ng_style, it seems I am using it correctly and Angular should handle the binding without explicitly updating the DOM with jQuery style.

If you have any suggestions on what I might be doing wrong or how to resolve this issue, or if there is a better or simpler approach to achieving the same functionality, please let me know.

Thank you, and feel free to request any additional files or information,

Sasha

Answer №1

ngStyle operates in a slightly different manner than the method you are currently using. It is evaluated as is, without the necessity of {{}} to reference a variable within $scope.

#full-page.fluid{ ng_controller: "ExerciseCtrl", ng_mousemove: 'updateSidebarWidth($event)', ng_mouseup: 'untrackMouseMove()', ng_cloak: true }
    .spacer
    .fixed-section-container
      .exercise-show
        %div
          .sidebar-section{ ng_style: 'sidebarWidthStyle' }
            .sidebar_header  

            .sidebar
              %div{ markdown: @exercise.body }
          .sidebar-toggle{ ng_mousedown: 'trackMouseMove()' }
          .work_area{ ng_style: 'workAreaWidthStyle' }

Using {{}} causes it to be interpreted as a string first before being processed by ngStyle. Consequently, ngStyle eventually monitors a string rather than a variable.

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

Utilize JavaScript to establish an object with key and value pairings

Wanting to loop through and create an object that contains key => [value], if the key already exists, simply add the value. Here is what I have tried so far: let dataName = []; let obj = {}; checkboxes.forEach(checkbox => { if (checkbox.che ...

Can a React/Redux/Saga application function without a backend server?

Before, I primarily used React/Redux/Saga for the "presentation" layer of my applications alongside Ruby/Rails and Node.js back-ends. Currently, I need to integrate with a couple of third-party back-end services (which are not under my control) for authe ...

What is the speed difference between calling functions from require's cache in Node.js and functions in the global scope?

Let's imagine a scenario where we have two files: external.js and main.js. // external.js //create a print function that is accessible globally module.exports.print = function(text) { console.log(text) } Now let's take a look at main.js: ...

Leverage environment variables within your index.html file

Currently, I am using Angular and I am encountering an issue while attempting to utilize an environment variable in my index.html for Google Analytics. I have attempted the following approach: <script> import {environment} from "./environments/e ...

Steps to establish a connection with a remote MYSQL database using the actual IP address in Node.js

In my Nodejs file, I have set up the connection as follows. The issue arises when attempting to connect to the remote database on a server. module.exports = { host: '234.32432.32432',//this is an example IP address and not a real one. use ...

What is the reason that the <script> tag cannot be directly inserted into the .html() method?

As I continue to learn front-end development, I've created my own version of JS Bin. When the 'run' button is clicked, a statement is executed to showcase the HTML, CSS, and JavaScript in an iframe (the output window): ("iframe").contents() ...

Generate an individualized rendering perspective

Hello, I am currently learning React and working on developing a Todo App. However, I have encountered an issue that has left me stuck. I need to figure out how to separate the value of [todos] into those marked as completed and pending. To-do List displ ...

Using $(document).ready() can sometimes cause issues with the functionality of the datatable

Currently, I am facing a challenge with initializing an empty table in the <tbody> section and sending parameters to the server using $.getJSON. The intention is to populate the table with the returned JSON data inside the <tbody>. It seems th ...

Conceal the cursor within a NodeJS blessed application

I am struggling to hide the cursor in my app. I have attempted various methods like: cursor: { color: "black", blink: false, artificial: true, }, I even tried using the following code inside the screen object, but it didn't work: v ...

Utilizing window.location.pathname in Next.js for precise targeting

Are you familiar with targeting window.location.pathname in NEXT.JS? I encountered a red error while using this code in Next.js const path = window.location.pathname console.log(path) // I am able to retrieve the pathname here Then { ...

The page refreshes automatically when the search icon is clicked, but the ajax method does not trigger the page reload

Whenever I click on the search-box <a>, the JavaScript is triggered but doesn't execute the ajax method filter_data_guide_specs(). Instead, the page automatically reloads and fails to read the JS code. HTML <div class="form-group"> < ...

What are some techniques for obtaining the second duplicate value from a JSON Array in a React JS application by utilizing lodash?

Currently, I am tackling a project that requires me to eliminate duplicate values from a JSON array object in react JS with specific criteria. My initial attempt was to use the _.uniqBy method, but it only retained the first value from each set of duplicat ...

Is it possible to retrieve the complete source HTML code of a webpage that is dynamically generated by JavaScript using Java and WebDriver?

I'm new to the world of programming and I've encountered a challenge that I need to overcome. I'm attempting to retrieve the HTML source code of a webpage using the Java / Webdriver method getPageSource(). However, the page seems to be dynam ...

The syntax for an AngularJS controller using the "as" keyword

Incorporating AngularJS 1.6.9 with AngularJS Material has been a challenge for me. I am attempting to integrate a menu item feature following the basic usage, but unfortunately, it did not work as expected due to structural differences. After reviewing th ...

Upgrading may lead to errors if react-dom is imported without being used

After recently updating react-dom to version 16.5.2, my tests have started to fail with the error message: requirejs error occurred TypeError: Cannot read property 'unstable_cancelScheduledWork' of undefined. Despite checking the release notes a ...

Vue.js component function "not instantiated within the object"

I recently started learning vue.js and decided to use vue-cli to set up a new project. As I was working on adding a method to a component, I encountered some issues: <template> <div> <div v-for="task in $state.settings.subtasks& ...

Async ngInit does not function properly when using $q promises

Edit: Plunker seems to be working fine, but the actual code is not functioning as expected. You can check it out here: http://plnkr.co/edit/5oVWGCVeuTwTARhZDVMl?p=preview The service contains the usual getter\setter methods and works well. I have omi ...

Angular JS is having an issue where the $routeParams object is mysteriously becoming

I've been working on the code Snippet below, but I'm having trouble retrieving values from $routeParams. It seems like $routeProvider is not being called. My goal is to click on a link, retrieve values from URL parameters, and change the page tit ...

Guide to conditionally adding a property to an object in JavaScript

After scouring both Stack Overflow and Google for a solution, I finally stumbled upon this brilliant idea: x = { y: (conditionY? 5 : undefined), z: (conditionZ? 5 : undefined), w: (conditionW? 5 : undefined), v: (conditionV? 5 : undefined), u ...

Mastering AngularJS: The ultimate guide to effectively binding service properties

Seeking the most effective method for binding to a service property in AngularJS. I have explored various examples to grasp how to bind to properties in a service created using AngularJS. Presented below are two examples demonstrating how to bind to serv ...