Angular.js - 防止直接访问部分视图

时间:2022-03-14 12:12:54

I want the users to not be able to view the partials directly when accessing their url in the browser. For example if my partial view is located in:

我希望用户在浏览器中访问其URL时无法直接查看部分内容。例如,如果我的部分视图位于:

/partials/myPartial.html

I can still directly access it and see the odd markup. Is there a way to prevent it and make the partial views only available through angular? Thanks.

我仍然可以直接访问它并看到奇怪的标记。有没有办法防止它,并使部分视图只能通过角度?谢谢。

2 个解决方案

#1


10  

Well, not really.

嗯,不是真的。

At the end of a day a browser needs to be able to access a partial to download it. If a browser can do this your users will be able to do so as well. You might eventually make it a bit harder for them to hit partials directly (for example to by configuring your server so it only responds to request with a certain header and configure $http to send this header on each XHR request).

在一天结束时,浏览器需要能够访问部分以下载它。如果浏览器可以执行此操作,您的用户也可以这样做。您最终可能会让它们更难直接命中部分(例如,通过配置您的服务器使其仅响应具有特定标头的请求并配置$ http以在每个XHR请求上发送此标头)。

The other possibility is to pre-load partials up-front as described here and not expose them via HTTP at all (actually this is a good practice for production deployments anyway).

另一种可能性是如前所述预先加载部分,而不是通过HTTP公开它们(实际上这对于生产部署来说是一个很好的做法)。

Otherwise it is hard to propose a meaningful solution without knowing what is the exact problem (functionally speaking) that you are trying to solve.

否则很难提出一个有意义的解决方案,而不知道你试图解决的确切问题(在功能上)。

#2


15  

I put a <script> at the top of every view partial that checks if the variable containing the main app Module exists, if not redirect to index.html

我在每个视图部分的顶部放置一个

If in app.js the appModule is defined in this way:

如果在app.js中appModule以这种方式定义:

var appModule = angular.module('myApp',["ngRoute","ngResource"]);`

At the top of the partial insert:

在部分插入的顶部:

<script type="text/javascript">
  if(typeof appModule === 'undefined'){
    document.location.href="index.html";
  }
</script>

#1


10  

Well, not really.

嗯,不是真的。

At the end of a day a browser needs to be able to access a partial to download it. If a browser can do this your users will be able to do so as well. You might eventually make it a bit harder for them to hit partials directly (for example to by configuring your server so it only responds to request with a certain header and configure $http to send this header on each XHR request).

在一天结束时,浏览器需要能够访问部分以下载它。如果浏览器可以执行此操作,您的用户也可以这样做。您最终可能会让它们更难直接命中部分(例如,通过配置您的服务器使其仅响应具有特定标头的请求并配置$ http以在每个XHR请求上发送此标头)。

The other possibility is to pre-load partials up-front as described here and not expose them via HTTP at all (actually this is a good practice for production deployments anyway).

另一种可能性是如前所述预先加载部分,而不是通过HTTP公开它们(实际上这对于生产部署来说是一个很好的做法)。

Otherwise it is hard to propose a meaningful solution without knowing what is the exact problem (functionally speaking) that you are trying to solve.

否则很难提出一个有意义的解决方案,而不知道你试图解决的确切问题(在功能上)。

#2


15  

I put a <script> at the top of every view partial that checks if the variable containing the main app Module exists, if not redirect to index.html

我在每个视图部分的顶部放置一个

If in app.js the appModule is defined in this way:

如果在app.js中appModule以这种方式定义:

var appModule = angular.module('myApp',["ngRoute","ngResource"]);`

At the top of the partial insert:

在部分插入的顶部:

<script type="text/javascript">
  if(typeof appModule === 'undefined'){
    document.location.href="index.html";
  }
</script>