Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've got an AngularJS app with pages built using Smarty. The top of the page has the ng-agg tag parameter, and basic AngualarJS templates are being compiled, and the .run method of my my module is being run, but my controller is not being called

Start of the html

<!DOCTYPE html>
<html ng-app="myapp">
...

$routeProvider

angular.module('myapp', function($routeProvider, $locationProvider) {
  $routeProvider
    .when('/index.php?page=tutorial', {controller: 'MyappTutorial'})
    .when('/index.php?page=home', {controller: 'MyappHome'})
    .when('/index.php?page=login', {controller: 'MyappLogin'})
})

Tutorial controller

var MyappTutorial = function ($scope, $location) {
    console.log('tutorial');
}

When I visit the page index.php?page=tutorial I my angular page template is built and a console.log() in my myapp.run() method is triggered, but the tutorial controller doesn't run.

How do I go about debugging this. I'm guessing it's the routeProvider going wrong, is there a way of me checking which controller is being used, if any.

I'm not getting any errors in my javascript console.

This is running in Chrome, from a local development Ubuntu virtual machine, on a Mac.

share|improve this question
 
I've managed to get around this by adding ng-controller=MyappTutorial type tags to each page, but I thought the route provider allowed me to avoid doing this –  Willshaw Media Oct 30 '12 at 0:43
add comment

1 Answer

up vote 3 down vote accepted

I think that you may want to reconsider how you are structuring your project. I tried something like you have and it doesn't seem right to me (and was unable to make it work).

I'm using php (outside of my job) and .net (at my job) with angular and the conclusion I came to, a while back, was that its far easier to work within the box, meaning, (IMHO) abandon smarty, abandon php templates and use angular as your client side and use php for your server side. Its not to say you can't have an index.php, however, you can use partials (php or html). You will find a much simpler structure whereby you have html, js (review tutorial routing and multiple views in the tutorials). You should realize that there are things you need to be aware of when you use partials (particularly on using # and html5mode).

However, you didn't ask for my opinion -- and I may get dinged for it... To answer your question. I think that if want to keep the structure that you have, then you should put the controller inside of your template attached to a container .

Hope this helps

--dan

share|improve this answer
 
I have wondered about ditching Smarty, but it's useful for building pages with repeatable components, like headers and footers. –  Willshaw Media Oct 30 '12 at 0:42
 
I still have smarty, but I use a bash script to build static HTML pages, everything is much simpler now. Thanks for the guidance Dan –  Willshaw Media Nov 13 '12 at 13:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.