2

I have the following structure of files:

  • index.html

  • tpl/page.html

  • app.js

Description:

  1. app.js has definition of main module with routing to page.html

  2. index.html defines view for template and includes app.js

  3. page.html has definition of PageController inside <script> tag and div who is using this controller.

Problem When PageController resides in page.html it doesn't work but when it is included into app.js then it works. Error is following: Argument 'PageController' is not aNaNunction, got undefined.

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. 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:

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

<md-content ng-controller="PageController" ...
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Alexey Anufriev
  • 415
  • 6
  • 19

1 Answers1

0

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.

My suggestion would be to have your controller in a separate JS file and set your Spring Security permissions on that. 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.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Vale H
  • 491
  • 5
  • 16
  • Could be an option but I have token mechanism of authorization so I need to intercept somehow browser's request to this JS file. And moreover since this it is a single page application and first unauthorized request will not provide token (when you just enter a page). But after authorization JS will not be requested again because I think you are proposing to include it into index page. – Alexey Anufriev Jan 19 '16 at 13:32
  • 1
    Yes, that was what I was proposing. I have no experience dynamically loading controllers like what you are wanting. The application I work on is more of a 2-page app. One for the public stuff and one for the protected stuff. Good luck on working it out. – Vale H Jan 19 '16 at 13:47