How to Access ViewBag Value from Asp.Net in AngularJs Controller

Just getting started with AngularJs and encountering an issue with passing a value from a .Net MVC View to an AngularJs Controller. Here's the code snippet in question:

AngularJs controller snippet:

app.controller("RatingApiController", function ($scope, RatingApiService) {

var id = $scope.roomId;
})

MVC View snippet:

<div id="tblSubs" ng-controller="RatingApiController" >
    <span ng-init="roomId = @ViewBag.roomId">something</span>
</div>

What is the best approach to access the roomId value from the Mvc View inside the id variable within the AngularJs controller?

Answer №1

Here is a helpful snippet for setting the roomId variable:

ng-init = "roomId = '@ViewBag.roomId'" 

You can also send the value using a function like this:

ng-init ="init('@ViewBag.roomId')"

If you have an init function in your controller, you can use it like this:

$scope.init = function(roomId) {

// do something with roomId
}

Answer №2

It's recommended to utilize an initialization function:

<div id="tblSubs" ng-controller="RatingApiController" >
    <span ng-init="init(@ViewBag.roomId)">something</span>
</div>

In the respective controller:

app.controller("RatingApiController", function ($scope, RatingApiService) {

    $scope.init = function(roomId) {
        // process the roomId here
    }
})

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

Does anyone know of a nodemon alternative that works on Windows?

Is there a Windows-compatible service similar to nodemon that can assist with Nodejs development now that it's available on the Windows platform? ...

The variable in Angular stopped working after the addition of a dependent component

Currently, I am working with Angular and have implemented two components. The first component is a navigation bar that includes a search bar. To enable the search functionality in my second component (home), I have added the following code: HTML for the n ...

What is the best way to mimic a library using SinonJs?

I am dealing with a file named browser-launcher.ts import * as Browser from "@lib/browser"; class BrowserLauncher { launch(options) { browser = Browser(options); } } export const browserLauncher = new BrowserLauncher() W ...

Tips for avoiding accidentally selecting nearby text while holding down a button on your mobile device

My application requires the user to press and hold a button to record audio. However, on mobile devices, when the user holds the button, the browser attempts to select nearby text due to finger pressure. While this behavior is understandable, I want to pre ...

The angular.js routing feature seems to be malfunctioning as the view is not loading properly

I'm facing an issue with my angularjs routing as the view is not rendering when a link is clicked. Surprisingly, there aren't any errors showing up in the console. Here's how I have set it up: (function(){ var app = angular.mod ...

Error: The URL type "workspace:" is not supported: workspace:*

I am embarking on a new project using Gatsby. You can create your project by running the following command: gatsby new YourProjectName2 https://github.com/Vagr9K/gatsby-advanced-starter However, I encountered an error message: info Creating new site fro ...

Failure to achieve success with jQuery Ajax

success in my AJAX call is not triggering at all, leaving me puzzled as to why. None of the alerts specified in the AJAX file are appearing. The form: <form onsubmit="check_reg();return false;" method="post" name="reg_form" id="reg"> <label ...

Retrieving data from a script within a React component

How can I access a variable from a script inside a React component? I am performing device detection on Node and sending the resulting object to the client (React) within index.ejs. <script type="text/javascript">window.deviceType = <%- deviceT ...

Enhance text by hovering over it

I am currently working on implementing a unique feature using jQuery and CSS. Rather than just inheriting the width, I want to create a magic line that extends to the next index item. Scenario: 1: Hover over Element one ELEMENT ONE ELEMENT TWO ELEM ...

Removing a selected row from a data table using an HTTP request in Angular 2

I am working with a table that has data retrieved from the server. I need to delete a row by selecting the checkboxes and then clicking the delete button to remove it. Here is the code snippet: ...

What is the syntax for invoking a javascript/typescript constructor while providing an object as an argument?

How should I structure my constructor if it will be invoked with the following code: const user = new User({ username, email }) Both `username` and `email` are of type string. Update: Here is how you can implement this in TypeScript: interface U ...

What is the best way to utilize a promise for a singular variable?

In my code, I have a requirement where I need to make two HTTP requests. The second request is dependent on information retrieved from the first request. Initially, the first request sets the variable 'amount' which is later used in the second re ...

Something is overriding the style created by makestyle material UI that I had implemented

How can I increase the importance of my makeStyles classes over default Material UI styles? Here is my code: import { createTheme, ThemeProvider } from '@mui/material/styles'; import { makeStyles, createStyles } from '@mui/styles'; co ...

Is it possible to deploy a single aspx.cs file using Visual Studio Web Publish to an Azure Web App, despite any runtime issues with Azure?

After deploying my ASP.NET web app to Azure using Visual Studio's web publish feature, I've encountered a strange issue. While I can easily deploy individual files by right-clicking and selecting publish in the solution explorer, changes to the c ...

What is the process for configuring socket.io to solely listen on a single designated route?

Is there a way to make socket.io listen only on my /home route, instead of every route? I tried changing the configuration but it only displayed a JSON file on the home path. const server = require('http').Server(app); const io = require('s ...

The information does not display in the AngularJS-generated table

Struggling with AngularJS directives? Let's take a look at the code: <div ng-controller="Controller"> <table> <thead> ....... </thead> <tfoot> ....... </tfoot> <tbody> < ...

Creating Swagger clients from scratch

I am working on a project that is based on npm and I am looking to add a swagger-based REST API client. My plan is to use a yaml file for the API description and generate the client during the build process. Are there any popular methods or tools for acc ...

Use jQuery to eliminate the <tr> elements from a table

I have a calendar that displays a list of appointments on the right side when a user clicks on a date. Although it is functioning correctly, the issue arises when the user clicks on another date, as the appointments from previous dates are still displayed ...

Utilize Vue: Bring in the router within a helper class and navigate to a specific

I'm new to utilizing Vue, and I am currently attempting to import my existing router instance into a JavaScript class where I manage the Authentication. This is the content of my router file: import Vue from 'vue'; import Router from &apos ...

AngularJS ui-router in HTML5 mode is a powerful combination that allows

Hey, I'm looking to implement HTML5 mode in my project. Here's how my file structure looks: mypage.de/mysub I can only make changes within the "mysub" directory. So far, I've added the following into my index.html: <base href="/mysub/ ...