4

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.

2
  • 1
    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 Commented Oct 30, 2012 at 0:43
  • adding the ng-controller='...' fixed my issue too Commented Feb 18, 2014 at 13:54

1 Answer 1

3

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

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

2 Comments

I have wondered about ditching Smarty, but it's useful for building pages with repeatable components, like headers and footers.
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

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.