简体   繁体   中英

AngularJS controller function ambiguity

I am new to angularjs and I have been trying out few basic tutorials and I noticed some discrepancy on how controller is declared and used thus I wanted clarification , for example in this JSfiddle link - http://jsfiddle.net/dakra/U3pVM/ the user has defined the controller as the function name which works perfectly fine for version 1.0.3 . I am using 1.3.15 version of angular and this approach isn't working for me

<html ng-app="myapp">

AngularJS data binding

<div data-ng-controller="SimpleController">

    Name :
    <br/>
    <input type="text" ng-model="name"/>{{name |uppercase}}
    <div>
        <ul>
            <li ng-repeat="personName in names">{{personName}}</li>
        </ul>

    </div>

</div>
<script src="node_modules/angular/angular.min.js"></script>

<script>

function SimpleController($scope) {
    $scope.names =['test1','test2','new'];
}

The above code just isn't working as it's showing an error of SimpleController being undefined function.

where as when I add this code instead of the above function, it works -

    var app = angular.module('myApp', []);
app.controller('SimpleController', function($scope) {
    $scope.names = ['test1','test2'];
});

Thanks,

The simple declaration of controllers via

function MyCtrl($scope) {

}

was removed in Angular 1.3. Breaking changes: http://wildermuth.com/2014/11/11/Angular_1_3_and_Breaking_Change_for_Controllers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM