Angular UI.Router's $state templates use $rootScope for ng-models

I'm encountering a strange issue where the ui-view template in my $state controller seems to be interacting with $rootScope instead of $scope.

However, it does initialize $scope variables properly.

For instance: if I have $scope.name = "test"

and in my template

<input ng-model="name">

the input will display "test" correctly.

But when I use $scope.$watch("name"), it doesn't trigger. Instead, only $rootScope watch on "name" gets triggered... Did I set up my routes incorrectly?

I have an main "app" that has its main dependency (my component):

angular.module('p1poc', ['ngAnimate', 'ngMaterial', 'ngWebSocket' ... 'myModule');

The module causing the issue is "myModule":

angular.module('myModule', ['ui.router', 'infinite-scroll', 'pasvaz.bindonce'])

  .config(['$stateProvider', function ($stateProvider) {
    $stateProvider.state('search', {
      url: '/search?query',
      controller: 'MainCtrl',
      templateUrl: 'main/main.html',
      label: 'Search',
      access: {free: true}
    });
  }])

As you can see, I am injecting the controller and template into it.

My template contains HTML elements like this:

  <md-select placeholder="Sort By" ng-model="orderBy">
                <md-option value="_score" checked>Relevance</md-option>
                <md-option value="citingsrcscount">Cited Count</md-option>
                <md-option value="sortdate">Publication Date</md-option>
              </md-select>

However, as mentioned earlier, the ng-model updates $rootScope.orderBy instead of $scope.orderBy.

But during initialization, it successfully retrieves data from $scope.orderBy...just not binding two ways

Answer №1

Make sure to set a null value for the $scope.name variable prior to utilizing it in your HTML code.

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

Dealing with unresolved promises in async/await functions

What is the best approach for handling unresolved promises? For instance: class Utils { static async thisFunctionOnlyResolvesWhenPassed2AndNeverRejects(number: number) { return new Promise((resolve, reject) => { if(number === 2 ...

What is the process for initializing the JWPlayer when it is embedded within Sitefinity?

We utilize a Sitefinity portal and have integrated the jwplayer on a specific page. Upon inspecting the page's HTML code, I only see a DIV element with an ID assigned to the video player. There is no indication of the javascript code present. However, ...

Reposition the initial element to the end of an array using setInterval() within a React component

I'm attempting to rearrange the elements in an array in React, specifically by moving the first item to the last position using a setInterval function: const [cardOrder, setCardOrder] = useState([card1, card2, card3, card4]); setInterval(() => { ...

What is the reason behind decorators needing to utilize apply(this) on a function?

I've been delving into the realm of JavaScript and exploring decorator code. One thing I've noticed is that when looking at decorator code like the example below, the input function always applies to 'this' even though it doesn't a ...

Updating the Progress Bar in Shopify when the "Add to Cart" button is clicked: A step-by

I'm currently working on implementing a shipping progress bar on my Shopify Theme. However, I've encountered an issue where the progress bar does not update correctly unless I refresh the page along with the content text. Can anyone provide guid ...

failure to properly assign a property during model update in mongoose

My BaseSchema contains logic that should set values for two properties when a new Model is created: schema.pre("save", function (next) { if (!schema.isNew) { this.createDate = new Date(); this.createBy = "kianoush"; } next(); }); If updating, ...

Alter text within a string situated between two distinct characters

I have the following sentence with embedded links that I want to format: text = "Lorem ipsum dolor sit amet, [Link 1|www.example1.com] sadipscing elitr, sed diam nonumy [Link 2|www.example2.com] tempor invidunt ut labore et [Link 3|www.example3.com] m ...

What is the best way to apply a hover rule to a CSS class in order to change the color of a specific row when hovering in

I have managed to successfully apply a classname and add color to a specific row in my react-table. However, I am facing difficulty in adding a hover rule to this className to change the color when I hover over the row. Can someone guide me on how to apply ...

Utilize the fetch function within a React functional component

I have been experimenting with different methods to fetch data only once before rendering, but I am encountering some challenges: It is not possible to call dispatch in componentDidMount as there is a restriction that it can only be done in Functional c ...

The data from my ajax code is not being successfully transmitted to the database

I'm having trouble using ajax to add data to the database. I'm not sure if the issue lies in the ajax code or the PHP code. Here is a snippet of my code: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></ ...

Struggling with inputting text in an AngularJS application

I am facing an issue with populating a text input with the output of a function in my HTML code. Despite trying different approaches, I am unable to figure out why it is not working correctly. Below is the snippet of my HTML code: <input type="text ...

Mapping three-dimensional coordinates to a two-dimensional screen location

My goal is to develop an interactive GUI for my app using threejs. I came across this informative tutorial: The tutorial provides the exact information I need, but it refers to an older release. function getCoordinates(element, camera) { var p, v, p ...

execute the function once the filereader has completed reading the files

submitTCtoDB(updateTagForm:any){ for(let i=0;i<this.selectedFileList.length;i++){ let file=this.selectedFileList[i]; this.readFile(file, function(selectedFileList) { this.submitTC(updateTagForm,selectedFileList); }); } } } ...

Is there a way to modify the parent component's state and pass it down to the child component as a prop efficiently?

I am facing an issue with a parent component that sets the score counter and passes it to the child component. There is a function in the parent component called resetBoard() which should reset the score counter back to 0 when triggered by a button click ...

The posenet TensorFlow model results in Node.js do not contain any position detail for coordinates "x" and "y"

I executed the posenet model on node v8.11.0. Below is the code snippet that I utilized. Unfortunately, the output results are not displaying the position of each pose; instead, it simply shows [Object]. It seems like there might be a canvas-related issue. ...

Issues with updating the style in Internet Explorer and Firefox using JavaScript are currently unresolved

I am facing an issue with my program where a name from an auto-complete is sent to a JavaScript function that dynamically creates a label with a button inside. Surprisingly, the DOM methods used to set style properties do not work in Firefox and IE 7, but ...

Ensuring Proper Tabulator Width Adjustment Across All Browser Zoom Levels

<div id="wormGearTabulatorTable" style="max-height: 100%; max-width: 100%; position: relative;" class="tabulator" role="grid" tabulator-layout="fitDataTable"><div class="tabulator-header" role="rowgroup"><div class="tabulator-header-co ...

Achieving consistent text alignment in HTML and CSS without relying on tables

As I delve into the world of HTML and CSS, I've taken a traditional approach by crafting a login page complete with three input controls (two text inputs and one button). To ensure proper alignment of these elements, I initially turned to the trusty & ...

Determine the necessary camera zoom level to ensure the object fits within the height of the screen

In my Three.js project, I've been utilizing these formulas to determine the visible width and height: var vFOV = camera.fov * Math.PI / 180; // converting vertical fov to radians var height = 2 * Math.tan( vFOV / 2 ) * dist; // calculating vis ...

Effortlessly create a seamless transition in background color opacity once the base image has finished

I've set up a div with a sleek black background. Upon page load, I trigger an API request for an image, which is then displayed in a secondary div positioned behind the main one. After this, I aim to smoothly transition the overlaying div's opaci ...