AngularJS没有SPA和使用ASP.NET MVC路由 [英] AngularJS without SPA and using ASP.NET MVC routing

查看:321
本文介绍了AngularJS没有SPA和使用ASP.NET MVC路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想与ASP.NET MVC一起使用AngularJS非SPA为我的网站。认证是通过Active Directory,所以我觉得最好使用ASP.NET MVC路由和认证/授权。

I want to use AngularJS non-SPA for my website along with ASP.NET MVC. The authentication happens through Active Directory so I find it best to use ASP.NET MVC for routing and authentication/authorization.

所以,我想要做的就是创建一个包含 NG-应用定义一个_Layout.cshtml页面,然后从_Layout.cshtml衍生每个视图将包含一个 NG-控制器定义。

So what I would like to do is to create a _Layout.cshtml page that contains a ng-app definition, then each view derived from _Layout.cshtml would contain a ng-controller definition.

有关REST风格的数据处理我会用的ASP.NET Web API。

For RESTful data handling I would use a ASP.NET Web API.

我能想到的唯一的问题是URL查询字符串参数。如果我谨<$ C $ç> ID 键查看一些具体的记录,我总是不得不把<$ C $ç> ID ViewBag 在ASP.NET MVC控制器和获得它在这样的观点:

The only problem I can think of are the URL query string parameters. If I would like to view some record specific by ID, I would always have to put the ID in a ViewBag in the ASP.NET MVC controller and obtain it in the view like this:

<script>
    var roomID = @ViewBag.roomID
</script>

有一些更好的方式来做到这一点?请一些线索。 :)

Is there some better way to do this? Please shed some light. :)

推荐答案

更新答案:

由于不使用Angularjs路由,可以使用 ngInit 。它会在您的控制器的范围内初始化一个新的变量,你将能够访问它,如:

Since you are not using Angularjs routing, you can use ngInit. It will initialize a new variable in your controller's scope and you will be able to access it like:

HTML(控制器内):

HTML (inside your controller):

<AnyElement ng-init="roomID = @ViewBag.roomID"></AnyElement>

JavaScript的:

JavaScript:

var roomID = $scope.roomID;

有关更多的解释请参见 ngInit文档

For more explanations see ngInit documentation.

原来的答复:

在Angularjs你可以很方便地访问查询字符串参数,如:

In Angularjs you can access the query string parameter very easily like:

var paramValue = $routeParams.yourParameterName;

或者,如果你想要初始化一个JavaScript变量那么可以考虑使用 ngInit 。它会在您的控制器的范围内初始化一个新的变量,你将能够访问它,如:

Or if you want to initialize a JavaScript variable then consider using ngInit. It will initialize a new variable in your controller's scope and you will be able to access it like:

HTML(控制器内):

HTML (inside your controller):

<AnyElement ng-init="roomID = @ViewBag.roomID"></AnyElement>

JavaScript的:

JavaScript:

var roomID = $scope.roomID;

有关更多的解释请参见 ngInit文档

For more explanations see ngInit documentation.

希望有所帮助。

这篇关于AngularJS没有SPA和使用ASP.NET MVC路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆