Angular JS controller not working

I couldn't figure out why the following code doesn't work at all. To be honest, I'm fine. Any idea?

<!DOCTYPE html>
<html data-ng-app="">
    <head>
        <title>Using AngularJS to create a simple Controller</title>
    </head>
    <body>
        <div data-ng-controller="simpleController">
            <ul>
                <li data-ng-repeat="cust in customers">{{ cust.Name | uppercase }} - {{ cust.City | lowercase }}</li>
            </ul>
        </div>
        
        <script
                type="text/javascript"
                src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.4/angular.min.js">
        </script>
        <script>
            function simpleController($scope) {
                $scope.customers = [
                    { Name: "Dave Jones",  City: "Phoenix" }
                    , { Name: "Jamie Riley", City: "Atlanta" }
                    , { Name: "Heedy Walhin", City: "Chandler" }
                    , { Name: "Thomas Winter", City: "Seattle" }
                ];
            }
        </script>
    </body>
</html>
      

Run codeHide result


+3


source to share


3 answers


It has to do with the version of angular you are using.

Earlier versions of angular allowed you to assign controller functions to the global scope as you did. This ability was then removed from angular. However, there are still many tutorials at this link in this older style.

See this demo - http://jsbin.com/fowamutoli/1/edit



I replaced angular legacy and your code works.

So in the future, you need to declare the angular module and register your controller against it. i.e.

<html data-ng-app="app">


 <script>

        var app = angular.module('app', []).  
            controller('simpleController', function ($scope) {
                $scope.customers = [
                    { Name: "Dave Jones",  City: "Phoenix" }
                    , { Name: "Jamie Riley", City: "Atlanta" }
                    , { Name: "Heedy Walhin", City: "Chandler" }
                    , { Name: "Thomas Winter", City: "Seattle" }
                ];
            });
  </script>

      

+8


source


https://docs.angularjs.org/guide/controller

try replacing your ng-app with ng-app = "myApp", see if it works. :) with the following snippet.



var myApp = angular.module('myApp',[]);

myApp.controller('simpleController', ['$scope', function($scope) {
  $scope.customers = [
                { Name: "Dave Jones",  City: "Phoenix" }
                , { Name: "Jamie Riley", City: "Atlanta" }
                , { Name: "Heedy Walhin", City: "Chandler" }
                , { Name: "Thomas Winter", City: "Seattle" }
            ];
}]);

      

+3


source


Try this instead:

<!DOCTYPE html>
<html ng-app="MyApp">
    <head>
        <title>Using AngularJS to create a simple Controller</title>
    </head>
    <body>
        <div ng-controller="simpleController">
            <ul>
                <li ng-repeat="cust in customers">{{ cust.Name | uppercase }} - {{ cust.City | lowercase }}</li>
            </ul>
        </div>

        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.4/angular.min.js"></script>
        <script>
            var myApp = angular.module( "MyApp", [] );
            myApp.controller("simpleController", function( $scope )
            {
                $scope.customers = [
                    { Name: "Dave Jones",  City: "Phoenix" }
                    , { Name: "Jamie Riley", City: "Atlanta" }
                    , { Name: "Heedy Walhin", City: "Chandler" }
                    , { Name: "Thomas Winter", City: "Seattle" }
                ];
            });
        </script>
    </body>
</html>

      

http://jsfiddle.net/rv7r7nv7/

+3


source







All Articles