Using scope variables in AngularJS version 1.4.8 with ng-style

index.html:

<script src="app.js"></script>
<div ng-app="app" ng-controller="app" ng-style="{ color: thecolor }">
  foo
</div>

app.js:

angular.module("app", [])
.controller("app", function() {
    $scope.thecolor = "red";
});

Fiddle.

Expected behavior was to render "foo" in red, but observed result showed it in the default black color. The issue arose when transitioning from Angular 1.0.2 to 1.4.8.

EDIT: Even after updating the controller to include the $scope dependency, certain functionalities such as positioning within a parent element still fail to work.

index.html:

<link rel="stylesheet" href="app.css">
<script src="app.js"></script>
<div ng-app="app" ng-controller="app">
  <div ng-style="{ left: x, top: y }">
    foo
  </div>
</div>

app.js:

angular.module("app", [])
.controller("app", ["$scope", function($scope) {
    $scope.x = 100;
    $scope.y = 100;
}]);

app.css:

body {
  margin: 0px;
}

div {
  position: relative;
  width: 100vw;
  height: 100vh;
}

div div {
  position: absolute;
}

Expected outcome was for "foo" to be rendered 100px down and to the right, but instead there was no displacement observed.

Fiddle.

Answer №1

The controller in your code is missing $scope as a Dependency Injection (DI). To address this, you can modify the code as shown below:

Updated Controller

angular.module("app", [])
 .controller("app", ['$scope',function($scope) {
    $scope.thecolor = "red";
    $scope.x = 100;
    $scope.y = 100;
}]);

View Integration

 <div ng-style="{ left: x + 'px', top: y + 'px' }">foo</div>

Check out the updated fiddle here

Answer №2

Remember to include units for your CSS values, which can be specified in the view:

<div ng-style="{ left: x + 'px', top: y + 'px' }">

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

Instability detected in the image when hovering over and off

Here is a snippet of code from my HTML file: <img id="id1" src="photo1" usemap="#ir" > <img src="photo2" id="id2"/> <span> <map name="ir" id="ir"> <area alt="" title="my title" href="my link" shape="poly" coords="13,15,29 ...

Best Practices and Tips for Layout Design Using HTML and CSS Grid

As I begin my journey into front-end development, I am currently immersing myself in studying html, css, and vanilla javascript. While things are going well so far, I find myself struggling with understanding the organization of design layouts in general. ...

Try implementing toggleClass() in the accordion feature rather than addClass() and removeClass()

Hey there! I've implemented accordion functionality using the addClass() and removeClass() methods. Here's a breakdown of what I did: <div class="container"> <div class="functionality">Accordion</div> <ul class="acco ...

Mixin for conditional input styles in Sass

I am interested in developing a conditional sass class that can be customized based on an argument provided. I am unsure about the terminology for this feature. For instance: <img class="width-200" .../> I would like to create a sass mixin that tak ...

The font from the server is not displaying correctly in the local HTML file

After uploading the ttf font file to the server, I utilized the following CSS code: @font-face { font-family: "fontname"; src: url("http://www.mywebsite.com/Fonts/fontname.ttf"); } body { font-family: "fontname", sans-serif; } Within the loc ...

The Django template fails to implement CSS styling

I am trying to apply text-align: justify and reduce the text width on a Django template file, but it seems that my CSS isn't working for only this specific element. I have no idea how to solve this issue even though my static files and direction are f ...

Conceal the Div containing the specified class

I was hoping to hide the first DIV if the second DIV is displayed on the front end, and vice versa upon page load. If the first DIV is set to 'block,' then the second DIV should be set to 'none.' And If the second DIV is set to &apos ...

The process of returning parameters from a modal view in Ionic

I am currently facing an issue with my modal view that is used for user login. I need to retrieve the user id from the modal and pass it back to the view behind. I attempted using $state.go but it was unsuccessful. Additionally, using ng-href would not wor ...

Changing the hover color of an Angular Material MD Button

<div class="non-active" layout layout-align='space-around center'> <md-button ng-click="$ctrl.widget.type = 'chart'; $ctrl.validateForm()" layout='column' ng-class="{selIcon : $ctrl.widget.type == ' ...

Revamping CSS for a Responsive Wordpress Tesseract Theme

I'm currently in the process of developing a website using the Tesseract theme at thevandreasproject.com. However, I have encountered an issue with responsiveness when integrating the SiteOrigins PageBuilder plugin. The compatibility between PageBuild ...

Filtering the Scope in AngularJS onClick Event

Looking to add an alert button to display the filtered object from my Angular filter data. I have successfully retrieved the desired object in my HTML template using: {{(portals|myFilter:or| filter:search )}} Here is my button: <a ng-href='#here& ...

The functionality of enabling and disabling dynamic behavior in AngularJs is not functioning as anticipated

As a newcomer to AngularJS, I may have some basic questions. I am currently working on implementing dynamic behavior for a button click event, but it's not functioning as expected. Could this be due to an issue with scope? Below is my HTML code: < ...

Exploring ways to integrate Javascript and CSS into a Bootstrap lightbox modal dialog?

Can someone help me figure out how to use the bootstrap lightbox to display a form with specific CSS style and JavaScript code for the form? I'm having some trouble implementing it. I specifically need this setup because I am using Selectize.JS to cr ...

Height adjustment for flexbox children

Can someone assist me with marking up a todo list? I am attempting to set the height of div.main-tasks equal to div.tasks so that the former fills the entire latter. Unfortunately, I am unsure how to achieve this. You can refer to the following image for c ...

Toggle the visibility of a div depending on the user's selection

My HTML select element has a few options to choose from: <select class="form-control" data-val="true" data-val-number="The field CodeSetup must be a number." id="CodeSetup" name="CodeSetup"> <option selected="selected" value="0">AllCodes< ...

Unable to reach the dropdown menu in CSS

Having trouble with creating a dropdown navigation bar? I've encountered an issue where the dropdown menu disappears as soon as I move my cursor off the buttons that reveal it. The menu itself displays properly, but accessing the drop down menu is pro ...

Having issues with resolving or rejecting promises when using $http get requests

I am facing a challenge in rejecting a deferred object within a $http.get call as it is not getting properly rejected. The error callback does not get triggered, and I'm struggling to understand why this is happening. Here is an outline of what my cod ...

Customizing WordPress using CSS overrides in the Pinboard theme

I'm currently customizing a WordPress theme for my website (pinboard theme) and so far, it's been an amazing experience. One of the features of this theme is a built-in slider/carousel. However, I am looking to change the title of the slider from ...

One specific JS file fails to load only when using Internet Explorer Browser in conjunction with the debugger

I am encountering an issue with a project that utilizes angular JavaScript controllers to populate fields. Unfortunately, a specific page, members.cshtml, is not loading properly in Internet Explorer 11, despite working perfectly on Chrome and Firefox. Af ...

Determine the exact width of text without rounding in Javascript

I'm facing an issue with a div element that I'm manipulating using Javascript to change its position. The problem is that it's rounding off incorrectly. #hi{ background-color: blue; width: 2px; height: 10px; position: absolu ...