Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Asp.Net ViewBag value in AngularJs controller

I'm new to AngularJs and I have a problem using a value from a .Net MVC View to a AngularJs Controller. Below is the relevant code:

AngularJs controller code:

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

var id = $scope.roomId;
})

Mvc View code:

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

What is the way to have the roomId value from the Mvc View in the id variable inside the AngularJs controller?

like image 490
sixfeet Avatar asked Jun 05 '26 01:06

sixfeet


2 Answers

Use this,

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

Or, sending value using a function

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

If you have a init funtion in controller

$scope.init = function(roomId) {

// do something with roomId
}
like image 135
Gaurav Amatya Avatar answered Jun 08 '26 00:06

Gaurav Amatya


Instead use an init function:

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

In your controller:

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

    $scope.init = function(roomId) {
        // do something with roomId
    }
})
like image 41
devqon Avatar answered Jun 08 '26 00:06

devqon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!