Проверьте мой код.
Почему ng-контроллер не работает?
<html data-ng-app="">
<head>
<title>Angular JS DEMO</title>
<style>
.container{
padding: 5px;
border: 1px solid #ccc;
margin: 2px 0px;
}
</style>
</head>
<body>
<div class="container" data-ng-controller="SimpleController">
<h3>Adding a Simply Controller</h3>
Name : <input type="text" data-ng-model="textbox_emp" /> {{textbox_emp}}
<br>
<ul>
<li data-ng-repeat="emp2 in emp_det">{{emp2.name}} - {{emp2.city}}</li>
</ul>
</div>
<script src="js/angular.min.js"></script>
<script>
function SimpleController($scope) {
$scope.emp_det = [
{name: 'Chinu Sahu', city: 'Bhubaneswar', age: '25'},
{name: 'Sanjib Pradhan', city: 'Cuttack', age: '28'},
{name: 'Aruna Tripathy', city: 'Jajpur', age: '26'},
{name: 'Debasis Das', city: 'Balasore', age: '30'}
];
}
</script>
</body>
</html>
Моя angular.min.js
версия: v1.4.1
Error: [ng:areq] http://errors.angularjs.org/1.4.1/ng/areq?p0=SimpleController&p1=not%20a%20function%2C%20got%20undefined
I/<@http://localhost/2015/test/AngularJS/AngularJS/js/angular.min.js:6:416
Sb@http://localhost/2015/test/AngularJS/AngularJS/js/angular.min.js:21:401
Qa@http://localhost/2015/test/AngularJS/AngularJS/js/angular.min.js:21:1
We/this.$get</<@http://localhost/2015/test/AngularJS/AngularJS/js/angular.min.js:79:1
y@http://localhost/2015/test/AngularJS/AngularJS/js/angular.min.js:59:501
N@http://localhost/2015/test/AngularJS/AngularJS/js/angular.min.js:60:339
g@http://localhost/2015/test/AngularJS/AngularJS/js/angular.min.js:54:386
g@http://localhost/2015/test/AngularJS/AngularJS/js/angular.min.js:54:409
g@http://localhost/2015/test/AngularJS/AngularJS/js/angular.min.js:54:409
S/<@http://localhost/2015/test/AngularJS/AngularJS/js/angular.min.js:53:444
zc/d/</<@http://localhost/2015/test/AngularJS/AngularJS/js/angular.min.js:19:357
hf/this.$get</m.prototype.$eval@http://localhost/2015/test/AngularJS/AngularJS/js/angular.min.js:134:394
hf/this.$get</m.prototype.$apply@http://localhost/2015/test/AngularJS/AngularJS/js/angular.min.js:135:104
zc/d/<@http://localhost/2015/test/AngularJS/AngularJS/js/angular.min.js:19:315
e@http://localhost/2015/test/AngularJS/AngularJS/js/angular.min.js:39:17
zc/d@http://localhost/2015/test/AngularJS/AngularJS/js/angular.min.js:19:236
zc@http://localhost/2015/test/AngularJS/AngularJS/js/angular.min.js:20:30
Yd@http://localhost/2015/test/AngularJS/AngularJS/js/angular.min.js:18:342
@http://localhost/2015/test/AngularJS/AngularJS/js/angular.min.js:289:159
a@http://localhost/2015/test/AngularJS/AngularJS/js/angular.min.js:175:303
Gf/c@http://localhost/2015/test/AngularJS/AngularJS/js/angular.min.js:35:71
...r a=[];n(arguments,function(b){a.push(d(b))});return e.apply(b,a)}:function(a,b)...
Есть ли у вас причина упускать свой модуль?
Если вы хотите создать контроллер, сначала вам нужно создать модуль.
(вы не можете использовать глобальный контроллер в угловом 1.4)
Вы можете создать модуль angularJS следующим образом:
angular.module('yourModuleName',[]);
и вы создаете контроллер на этом модуле.
как это,
angular.module('yourModuleName').controller('yourControllerName',function($scope)
{ ... your controller logic});
Я исправил ваш код на основе этой техники (см. Выше).
<html data-ng-app="myApp">
<head>
<title>Angular JS DEMO</title>
<style>
.container{
padding: 5px;
border: 1px solid #ccc;
margin: 2px 0px;
}
</style>
<script src="angular.min.js"></script>
<script>
angular.module('myApp',[]);
angular.module('myApp').controller('SimpleController',SimpleController);
function SimpleController($scope) {
$scope.emp_det = [
{name: 'Chinu Sahu', city: 'Bhubaneswar', age: '25'},
{name: 'Sanjib Pradhan', city: 'Cuttack', age: '28'},
{name: 'Aruna Tripathy', city: 'Jajpur', age: '26'},
{name: 'Debasis Das', city: 'Balasore', age: '30'}
];
}
</script>
</head>
<body>
<div class="container" data-ng-controller="SimpleController">
<h3>Adding a Simply Controller</h3>
Name : <input type="text" data-ng-model="textbox_emp" /> {{textbox_emp}}
<br>
<ul>
<li data-ng-repeat="emp2 in emp_det">{{emp2.name}} - {{emp2.city}}</li>
</ul>
</div>
</body>
Надеюсь, вы это поняли!
Если вы новичок в angularJS, я рекомендую книгу под названием "AngularJS Novice to Ninja".
Попробуй!
Глобальный контроллер не действителен для угловых 1.3 или выше
используйте модули: - <html data-ng-app="my">
angular.module('my',[]).controller('SimpleController',function($scope) {
});
Вот плункер