简体   繁体   English

将AngularJS 1.2集成到现有的MVC网站

[英]Integrating AngularJS 1.2 into existing MVC site

I currently have an existing ASP MVC4 application that returns views (razor) from controllers and would like to start transitioning the application into a SPA (single page application) using web api and angular js. 我目前有一个现有的ASP MVC4应用程序,该应用程序从控制器返回视图(剃刀),并希望开始使用Web api和angular js将其转换为SPA(单页应用程序)。 My question is can i slowly start transitioning specific views/urls to be routed by angular and have other views/urls call back to the legacy controllers in the MVC application that render razor views? 我的问题是,我可以慢慢开始过渡特定的视图/ URL以便通过角度路由,并让其他视图/ URL回调到呈现剃刀视图的MVC应用程序中的旧控制器吗? For example I have my main angular js file stating the following 例如,我有我的主要角度js文件,说明以下内容

window.EcentralSPA = angular.module('EcentralSPA', ['ngRoute']);

EcentralSPA.config(['$routeProvider', function ($routeProvider) {
  $routeProvider.when('/users', {
    templateUrl: 'app/views/users.html',
    controller: 'UsersController'
  });
}]);


EcentralSPA.controller('UsersController', ['$scope', function ($scope) {
  //TODO
}]);

Basically i'd just like the /users url to get handled by angular and the rest of the urls to call back to the web server to the correct mvc controller and render back the razor view. 基本上,我只是想让/ users网址由angular处理,其余的网址则要回调Web服务器到正确的mvc控制器并返回剃刀视图。 However with my current configuration above all calls to anything besides /users returns empty data. 但是,根据我当前的配置,除/ users之外的所有调用均返回空数据。 Any suggestions? 有什么建议么? Thank you. 谢谢。

If you decide part of application is SPA and another is not for some url, you need to then stop AngularJS from intercepting url changes. 如果您确定应用程序的一部分是SPA,而另一部分不是用于某些URL,则需要停止AngularJS拦截URL更改。 For this when you render <a> tags use one of the following ways to ignore angularjs routing 为此,当渲染<a>标签时,请使用以下方法之一来忽略angularjs路由

  • Links that contain target element. 包含目标元素的链接。 Example: <a href="/ext/link?a=b" target="_self">link</a> 示例: <a href="/ext/link?a=b" target="_self">link</a>
  • Absolute links that go to a different domain. 指向不同域的绝对链接。 Example: <a href="http://angularjs.org/">link</a> 示例: <a href="http://angularjs.org/">link</a>
  • Links starting with '/' that lead to a different base path when base is defined. 以'/'开头的链接在定义base时导致不同的基本路径。 Example: <a href="/not-my-base/link">link</a> 示例: <a href="/not-my-base/link">link</a>

See the location documentation for more details. 有关更多详细信息,请参见位置文档

Also i don't think you can then perform navigation using $route or $location service for urls that you want to handle outside angularjs. 另外,我不认为您可以使用$route$location服务对要在angularjs之外处理的网址执行导航。

Full page refresh will always cause the full framework to reload for all AngularJS SPA pages. 全页刷新将始终导致为所有AngularJS SPA页重新加载完整的框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM