AngularJS templates don't work with Spring - angularjs

I have developed a REST application using Spring in NetBeans IDE.
Here is the relevant dir structure:
I want to integrate Angular functionality into it, but will prefer to keep it as a single app, rather than separate Angular and Spring apps.
The main index.html file should show the template home.html on the front page.
The Problem:
Adding HTML files to the templates folder doesn't seem to work in the Angular application. If I try to access the index.html and home.html files through Angular, I get a 404 error, but I can open them directly.
Here's the controller in my Spring application for these two files:
#RequestMapping("/")
public String index() {
return "index";
}
#RequestMapping("/home")
public String home() {
return "home";
}
I have written AngularJS code before by itself, and I didn't have any problems.
Am I mixing some things that I shouldn't?
Do I create all my AngularJS code in a separate folder, away from src/main/resources?

Spring maps your files placed in src/main/resources/static to the root directory of your application. This means a CSS file in your src/main/resources/static/css/ folder can be accessed by simply using the /css relative path.
Static resources such as CSS, Javascript, templates or images should therefore be placed in the static folder, where Angular will be able to access it.
This restriction does not extend to your main index.html file, and you can place it in the main project directory or under templates.
Answer taken from JBNizet in comments.

Related

Deploying Angular Application to GitPages - Not Routing Properly

Disclaimer: I am not a frontend guy by trade. I am being asked to deploy an Angular application other engineers have created.
I have gone through the process of deploying an Angular application to GitPages and have also tried the handy Angular CLI GH Pages library. However, they both have the same issue, and I'm not sure if it's the application itself or how I am deploying it.
The base page loads fine, but all other resources (image/font files, links to other pages, etc.) do not load properly. This is because they are not using the proper base URL; our corporate GitHub makes us use https://....com/<org>/<repo> as the URL. All resources outside of the base page are not prepending the /<org>/<repo>/ part to the URL, so they are all returning 404s.
My index.html file in docs/ contains the proper base href="/<org>/<repo>/" and the docs/ folder contains all needed images and font files in its assets/ subdirectory, so I'm not sure what gives. I have also copied index.html to 404.html.
Am I missing something? Or is it possible that this web application was not created correctly for GH Pages?
Thanks in advance :-)
Change the base href to index.html only and all will work from there
base href="index.html"
From there it will take the path properly

Where to put ng1 templates in an angular hybrid application built by angular-cli?

I'm trying to build an angular hybrid application out of a angular.js application. I'm following the official documentation (https://angular.io/guide/upgrade). I reached and completed this step: https://angular.io/guide/upgrade#bootstrapping-hybrid-applications.
I'm using angular ui-router 0.3.1 to handle the routing of my application. Therefore, using the config function on my main module, and the $stateProvider service, I've mapped each route to an angular.js controller and a template specified by the templateUrl parameter.
My application seems to startup correctly but the templates cannot be loaded because they cannot be found (404 error). This is normal because they are not imported in the dist folder where my application is built when I use the ng build angular-cli command line.
How can I configure my application so that my angular.js html templates get copied in the dist folder when my angluar application is built by angular-cli?
Thanks in advance!
So actually it appears my question was a duplicate of that one: Angular CLI with Hybrid app ng-build.
The idea is to use the assets array from angular-cli.json. Glob enables to select recursively all html files from the folder of your choice and to put them in a subfolder of dist:
"asssets": [{"glob": "**/*.html", "input": "../src-ng1", "output": "./src-ng1"}]

Play framework and Angular - Loading angular templates

I have an Angular app which I am serving from Play.
The main routes within Play all serve some HTML pages which contain ng-include statements to some angular templates which are in a "templates" folder in public...
<div ng-include="'#routes.Assets.versioned("templates/test.html")'"></div>
This works fine as I can use Play to reference the assets as normal. Now when the page loads in the browser, each of these templates load and in turn try to pull in further angular templates...
<div ng-include="'templates/test2.html'"></div>
But this results in a 404 as the "templates" folder cannot be resolved.
How can I have a truly public "templates" folder with Play where each of angular can pull in whatever templates it needs in the browser?
Thanks
You will need something like this in your PLAY routes file:
GET /templates/*file controllers.Assets.at(path="/public/templates", file)
In this link you can see the whole solution

How do you point to an AngularJS Template from within a JS file provided by a Grails plugin?

I have a Grails app which pulls in several Grails Plugins. These plugins need to be reused by several other Grails apps not just my own.
In a Resources Bundle in one of the Grails Plugins I have the following defined:
leaving this for completeness, but I have since switched to the Asset-Pipline
modules = {
directivea {
resource url: 'directives/directivea/directivea.js'
resource url: 'services/restapis.js'
}
}
In a Javascript file in one of the plugins I have the following directive defined:
ModuleA.directive('directivea',function() {
return {
restrict: 'EA',
scope: {
'objId' : '='
},
replace: true,
link: function(){
$scope.ObjId = attributes[''];
$scope.someFunction($scope.ObjId);
},
controller: function(){
$scope.someFunction = function(objId){
//some stuff happens here
};
},
templateUrl: 'directives/directivea/directivea.html'
}
});
It seems to be executing the controller just fine but when it tries to pull in the template it chokes on:
GET /appname/directives/directivea/directivea.html
404 (Not Found)
This makes sense because that's not where the partial template will be. Where would it be though? How can I keep that information isolated to within the plugin but usable by all the downstream applications? I'd like to avoid defining in-line templates if I can.
**EDIT TO INCLUDE STRUCTURE**
Structure of Plugin:
grails-app
conf
PluginNameForGrailsResources.groovy
BuildConfig.groovy
controllers
PluginControllerA.groovy
PluginControllerB.groovy
domain
PluginDomainA.groovy
web-app
css
directives
directivea
directivea.js
directivea.html
services
restapis.js
application.properties
PluginNameForGrailsGrailsPlugin.groovy
Structure of the application referencing my Plugin:
grails-app
conf
BuildConfig.groovy
controllers
domain
views
layouts
main.gsp
index.gsp (references to directivea via resources r:require tag)
web-app
css
main.css
js
index.js
application.properties
**EDIT TO INCLUDE STRUCTURE POST-ASSET-PLUGIN-SWITCHOVER**
no more Resources defined, BuildConfig.groovy now references the asset pipline instead of Resources, no more files in web-app
grails-app
conf
BuildConfig.groovy
controllers
PluginControllerA.groovy
PluginControllerB.groovy
domain
PluginDomainA.groovy
assets
javascripts
directivea
directivea.js
directivea.html
services
restapis.js
application.properties
PluginNameForGrailsGrailsPlugin.groovy
Using the Grails Resource plugin
Refer this sample grails app which uses this grails plugin.
Plugin hosts an angular module. I tried to keep it simple by just adding a directive and no other module. Also note that I have used static/.. in the templateUrl in directive.
Also refer Config.groovy in the app which uses below config
grails.resources.adhoc.includes = ['/images/**', '/css/**', '/js/**', '/img/**']
as mentioned in the answer to this question. May be you are hitting this issue.
Give it a try. "Hello World" is rendered from the directive present in an angular module inside the plugin.
If you are using latest version of Grails then I would suggest to switch to asset pipeline whenever convenient.
Using the Grails Asset Plugin
Use the Grails asset-pipeline plugin (v 1.9.7 as of this posting) and the Grails Angular-template-asset-pipeline plugin (v 1.2.5 as of this posting).
For details on exactly how to set this up, see the answers to How to reference a static HTML resource using the Grails Asset Pipeline plugin.
Primarily, ensure your Angular JS module is lowercase or uses _'s or -'s in the name and that your template file has a slightly different filename (ignoring the extension) than any of the javascript assets you're including.
Note that the javascript is referencing things from the web-app root, not from the resources or assets directory. You can find more about Angular routing from the core documentation: $route
So templateUrl: directives/directivea/directivea.html needs to actually reference a file in the /web-app/directives/directivea/directivea.html directory.
Note that the resources plugin does not seem to be very modular, and global files need to be specified for sharing (and most other things). If you move to grails 2.4 and the assets plugin, you can do modular file paths.

How to use Laravel 4 localization methods in Angular app?

Is there a way for you to use Laravel trans() or Lang::get() method using (lang folder) in AngularJS app?
Laravel view that displays an angular view with multiple controllers.
<div ng-view></div>
I have in public_html folder all my templates for my app.
With the default structure of a Laravel app you can't access the app/lang directory. However, you could write a grunt task or a Laravel command, that process all you language files and copy the results in your public directory to make it publicly available for your Angular app.

Resources