NPanda NPanda - 1 month ago 8
AngularJS Question

MVC Spring AngularJS Directive and Controller

I am trying to get a MVC with Spring and Angular JS and it is currently working [well actually it just calls the index.html so yeah].
The thing is, when I am in my index.html, I simply put a ng-include to another html file like this :

<div ng-include="'folder/view.html'"></div>


The thing is... I would like to call the directive of the file first, which will call the controller right after and then call the view but I do not know how to proceed.

Here is my directive :

'use strict';
angular.module('tmp')

.directive('view', function() {

return {
restrict: 'AE',
templateUrl: "folder/view.html",
controller : "viewController",
scope: {
state: "@",
},

}
});


And here is my controller :

'use strict';
angular.module('tmp')
.controller('viewController', function ($scope) {

// Do some stuff

});


But none of these two are called. Can someone help me please ?
I do not know if I am specific enough, I'd like to apologize in advance.

Plunker : https://plnkr.co/edit/EFScNxJV7qG9cTSACDZ2?p=preview

Thank you !

Answer

Well, you of course need to load the JS files defining the controller and the directive:

<script src="script.js"></script>
<script src="viewController.js"></script>
<script src="viewDirective.js"></script>

Then you need to actually use the directive in your template:

<body>
  <view></view>
</body>

And finally, since the view.html is not in a folder, you need to use the right path.

https://plnkr.co/edit/DygHtWNMtWTG7hy4vBUb?p=preview