AngularJS ng-controller not working

前端 未结 8 1407
日久生厌
日久生厌 2020-12-09 16:30

I just now started learning on AngularJS from w3schools. I am trying to practice examples what ever they have mentioned in the tutorials. Every thing works fine but when i c

相关标签:
8条回答
  • 2020-12-09 17:21

    This is a common search result for "Angular Controller Not Working" so I thought it'd be helpful to add the solution that fixed my "controller not working" problem.

    I had my base route with $routeProvider pointing at a controller file and a templateUrl partial like so:

    $routeProvider.when('/', {
        templateUrl: 'partials/home.html',
        controller: 'homePage'
    });
    

    I had several other routes and all those controllers worked, but for some reason my first route wouldn't. Turns out, I had a small logic error in my partial and that prevented ANY of my controller code from running.

    Dumb little mistake, but I was caught off guard that it was a logic issue in my partial that was preventing any controller code from running. It took a lot longer than I'd like to admit to find because the issue wasn't in my "JavaScript" even though the console errors were present. The joys of tightly coupled front-end code, amiright?

    0 讨论(0)
  • 2020-12-09 17:24

    This is your corrected fiddle.

    It is a good practice for angular that the controller definition must look something like this:

    angular.module("app", []).controller("personController", function($scope) {
        $scope.firstName = "John";
        $scope.lastName = "Doe";
    });
    

    And, without doubt, the best tutorial ever for learning the basics of Angular is the CodeSchool one!

    Hope this helps.

    0 讨论(0)
提交回复
热议问题