我只是角色的初学者。正在制作这个小代码。但它不起作用.Ng-repeat除了显示数组但不是。请帮助。 以下是代码:
<!DOCTYPE html>
<html data-ng-app="">
<head>
<title>Using Angulatjs directives and Data Binding</title>
</head>
<body>
<div data-ng-controller="SimpleController">
Name:
<br>
<input type="text" data-ng-model="name" />
<h3>Using ng-repeat and filters</h3>
<ul>
<li data-ng-repeat="cust in customers"> {{cust.name}}-{{cust.city}} </li>
</ul>
</div >
<script type="text/javascript" src="angular.min.js"></script>
<script>
function SimpleController($scope)
{
$scope.customers= [
{name:'John Smith',city:'Phoenix'},
{name:'John Doe',city:'New York'},
{name:'Jane Doe',city:'Sansfransico'}
];
}
</script>
</body>
</html>
[
我在console.Screen镜头上也收到了以下错误。
angular.min.js:117错误:[ng:areq] http://errors.angularjs.org/1.5.5/ng/areq?p0=SimpleController&p1=not%20a%20function%2C%20got%20undefined 在错误(本机) 在file:///home/shallow/60/angular.min.js:6:412 在qb(file:///home/shallow/60/angular.min.js:23:157) 在Pa(文件:///home/shallow/60/angular.min.js:23:244) 在file:///home/shallow/60/angular.min.js:89:77 在O(文件:///home/shallow/60/angular.min.js:72:75) at n(file:///home/shallow/60/angular.min.js:64:7) at g(file:///home/shallow/60/angular.min.js:58:305) at g(file:///home/shallow/60/angular.min.js:58:322) at g(file:///home/shallow/60/angular.min.js:58:322)(匿名函数)@ angular.min.js:117
截图:
[1]: http://i.stack.imgur.com/CYbHp.png
答案 0 :(得分:0)
为什么不在data-ng-app
属性上设置值来命名主模块,如myApp
然后,在脚本标记内定义控制器。 控制器应该属于您之前在HTML标记上定义的主模块。
<!DOCTYPE html>
<html data-ng-app="myApp">
<head>
<title>Using Angulatjs directives and Data Binding</title>
</head>
<body>
<div data-ng-controller="SimpleController">
Name:
<br>
<input type="text" data-ng-model="name" />
<h3>Using ng-repeat and filters</h3>
<ul>
<li data-ng-repeat="cust in customers"> {{cust.name}}-{{cust.city}} </li>
</ul>
</div >
<script type="text/javascript" src="angular.min.js"></script>
<script>
angular.module('myApp', [])
.controller('SimpleController', SimpleController);
function SimpleController($scope)
{
$scope.customers= [
{name:'John Smith',city:'Phoenix'},
{name:'John Doe',city:'New York'},
{name:'Jane Doe',city:'Sansfransico'}
];
}
</script>
</body>
</html>
答案 1 :(得分:0)
您使用的是什么版本的angular
?您的代码可以与angular 1.3.0
一起使用。更高版本的angular不允许声明全局函数。这是您的代码版本的工作plunker。
http://plnkr.co/edit/aDhWAC2YFoH64Srr4oUV?p=preview
我正在使用角度为1.3.0的CDN。
整体代码:
<!DOCTYPE html>
<html data-ng-app="">
<head>
<script src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>
</head>
<body>
<div data-ng-controller="SimpleController">
Name:
<br>
<input type="text" data-ng-model="name" />
<h3>Using ng-repeat and filters</h3>
<ul>
<li data-ng-repeat="cust in customers"> {{cust.name}}-{{cust.city}} </li>
</ul>
</div >
<script>
function SimpleController($scope)
{
$scope.customers= [
{name:'John Smith',city:'Phoenix'},
{name:'John Doe',city:'New York'},
{name:'Jane Doe',city:'Sansfransico'}
];
}
</script>
</body>
</html>