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?
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
}
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
}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With