0

I am following this tutorial, and I have an app structure like this. I've tried to show only the relevant bits as it is sort of a lot of code.

/app
    /views
        index.ejs
/config
    express.js
/public
    /external_libs
        angular.js
        angular-ui-router.js
    /js
        app.js
        controllers.js
    /partials
        home.html
server.js

Inside my express.js (relevant bit)

app.use(express.static('./public');

I am able to set up my angular controllers, so I know this directory is being hit. For example, my index.ejs

<html ng-app="myapp">
    <head>
        <script src="external_libs/angular.js"></script>
        <script src="external_libs/angular-ui-router.js"></script>
        <script src="js/app.js"></script>
        <script src="js/controllers.js"</script>
    </head>
    <body ng-controller= "MainCtrl"> <!-- an alert in my controller fires, so I know the public directory is accessible, at least the js folder-->
        <div ui-view></div>
    </body>
</html>

In my app.js

var app = angular.module('myapp', ['ui.router']);
app.config([
    function($stateProvider, $urlRouterProvider) {
        $urlRouterProvider.otherwise('/home');
        $stateProvider
            .state('home', {
                url: '/home',
                templateUrl: 'partials/home.html'
            });
    }
]);

In controllers.js

var app = angular.module('myapp', []);
app.controller('MainCtrl', [
    '$scope', function($scope) {
        alert("This alert is indeed alerted");
    }
]);

home.html

<h1> This is a test to see if the view on index.ejs is being populated </h1>

I have tried many different combinations for the "templateUrl" inside app.js, including

"partials/home.html"
"/partials/home.html"
"../partials/home.html"

None of these result in home.html being placed inside the div ui-view element on my index.ejs page. I realize I have posted a somewhat limited amount of code, but the fact that I am able to hit my controllers and see an alert message leads me to believe that I am almost there. I am using server side routing to render the initial index.ejs, but other than that I want to handle things client side. Does anyone know how to make angular-ui-router locate my partial with this set up?

8
  • And if you replace your templateUrl with a template:'<div>test</div>'? Also looking at the requests (dev tools network-tab) might give a hint to what files it's trying to fetch. Commented Aug 25, 2015 at 5:13
  • replacing with template '<div>test</div>' does not give me the partial :/ Commented Aug 25, 2015 at 5:18
  • are you actually trying to load yourdomain.com/#/home? Commented Aug 25, 2015 at 5:22
  • just tried it, no luck. Commented Aug 25, 2015 at 5:23
  • can you share more of your code (say in a fiddle)? Commented Aug 25, 2015 at 5:25

1 Answer 1

1

The problem is with your controller declaration. Rather than referencing the module you recreate it (and override the existing module) by including the square brackets.

If you rewrite your controller as below it should work:

var app = angular.module('myapp');
app.controller('MainCtrl', [
  '$scope',
  function($scope) {
    alert("This alert is indeed alerted");
  }
]);

For more info, check out "Creation versus Retrieval" at https://docs.angularjs.org/guide/module

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.