简体   繁体   English

如何在HTML模板中定义AngularJS控制器

[英]How to define AngularJS controller in HTML Template

I have the following structure of files: 我具有以下文件结构:

  • index.html 的index.html

  • tpl/page.html TPL / page.html中

  • app.js app.js

Description: 描述:

  1. app.js has definition of main module with routing to page.html app.js通过路由到page.html定义了主模块

  2. index.html defines view for template and includes app.js index.html定义模板的视图并包含app.js

  3. page.html has definition of PageController inside <script> tag and div who is using this controller. page.html在<script>标签和使用此控制器的div中具有PageController的定义。

Problem When PageController resides in page.html it doesn't work but when it is included into app.js then it works. 问题当PageController驻留在page.html中时,它不起作用,但是当它包含在app.js中时,它就起作用了。 Error is following: Argument 'PageController' is not aNaNunction, got undefined. 错误如下:参数'PageController'不是aNaNunction,未定义。

It looks like it is too late to define controller in template. 在模板中定义控制器似乎为时已晚。 But actually it is my requirement. 但这实际上是我的要求。 I want to hide page.html + controller (even their code) from all users and make it visible only to particular group by Spring security. 我想对所有用户隐藏page.html +控制器(甚至是他们的代码),并通过Spring安全使其仅对特定组可见。 That is why I defined security constraint for this page and want it to be executed only when this constraints are met. 这就是为什么我为此页面定义了安全性约束,并希望仅在满足此约束时才执行它。

Update #1 Controller definition: 更新#1控制器定义:

<script type="text/javascript">
  mainApp.controller('PageController', function($scope, $rootScope) {
    ...
  });
</script>

<md-content ng-controller="PageController" ...

If both the script tag and the HTML for the template are defined in an external html file, Angular should complain and say that you cannot have two root elements in a template file. 如果脚本标记和模板的HTML都在外部html文件中定义,则Angular应该抱怨说模板文件中不能包含两个根元素。

My suggestion would be to have your controller in a separate JS file and set your Spring Security permissions on that. 我的建议是将您的控制器放在单独的JS文件中,并在其上设置Spring Security权限。 If you are using JSPs or something similar to render the main page, you could then add a conditional around the <script> tag for the controller if the user has access. 如果您正在使用JSP或类似的方法来渲染主页,则可以在用户具有访问权限的情况下,在控制器的<script>标记周围添加条件。

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

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