Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
145 views
in Technique[技术] by (71.8m points)

javascript - angular 1.3 can't find the controller function

I am new to angular and tried to go with 1.3 new release.
Here is my code

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.0/angular.min.js"></script>
</head>
<body>
<div data-ng-controller="SimpleController"  >

<ul>        
<li data-ng-repeat="customer in customers">{{customer.name}} - {{customer.city}}</li>
</ul>


</div>
<script>

function SimpleController($scope){

    alert('done1');
    $scope.customers=[{name:'1name',city:'1city'},{name:'2name',city:'2city'}];
    alert('done');
} 
</script>

The console gives this error.

Error: [ng:areq] http://errors.angularjs.org/1.3.0-rc.0/ng/areq?p0=SimpleController&p1=not%20a%20function%2C%20got%20undefined    

But when I change the angular source to

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>   

Then it works. I use chrome. any one let me know where can be the problem exists.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Global controller functions are no longer supported by default in 1.3. See change log...

$controller will no longer look for controllers on window. The old behavior of looking on window for controllers was originally intended for use in examples, demos, and toy apps. We found that allowing global controller functions encouraged poor practices, so we resolved to disable this behavior by default.

It can be re-enabled with this config...

angular.module('myModule').config(['$controllerProvider', function($controllerProvider) {
  // this option might be handy for migrating old apps, but please don't use it
  // in new ones!
  $controllerProvider.allowGlobals();
}]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...