在AngularJs控制器中使用Asp.Net ViewBag值

时间:2016-12-06 15:42:38

标签: javascript asp.net angularjs asp.net-mvc

我是AngularJs的新手,我在使用.Net MVC View中的值到AngularJs控制器时遇到了问题。以下是相关代码:

AngularJs控制器代码:

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

var id = $scope.roomId;
})

Mvc查看代码:

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

在AngularJs控制器中的 id变量中,从Mvc View 获取 roomId值的方法是什么?

2 个答案:

答案 0 :(得分:3)

使用此,

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

或者,使用函数发送值

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

如果你在控制器中有一个初始功能

$scope.init = function(roomId) {

// do something with roomId
}

答案 1 :(得分:2)

而是使用init函数:

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

在您的控制器中:

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

    $scope.init = function(roomId) {
        // do something with roomId
    }
})