您好我已经开始从strach学习AngrularJS,我对基本的两个模块和基本过滤器有问题:
<body ng-app="myapp">
<div ng-controller="MyController" >
{{1+1}}
<div>Filtered: {{myData.text | myFilter:2:5}}</div>
</div>
<script>
angular.module("myapp", [])
.controller("MyController", function($scope) {
$scope.myData = {};
$scope.myData.text = "balsalaskdlas";
});
angular.module("myapp", []).filter('myFilter', function() {
return function(stringValue, startIndex, endIndex) {
return stringValue.substring(parseInt(startIndex), parseInt(endIndex));
};
});
</script>
</body>
但是这段代码给了我错误:“Argument'MyController'不是函数,未定义”
在HTML浏览器中,我看到了以下页面:
{{1+1}}
Filtered: {{myData.text | myFilter:2:5}}
这些代码有什么问题?
答案 0 :(得分:2)
angular.module("myapp", [])
上面一行定义了一个名为&#34; myapp&#34;的新模块,并覆盖了之前定义的模块。如果您想获得对模块的引用并为其添加过滤器,请使用
angular.module("myapp").filter(...)
答案 1 :(得分:1)
您已经两次宣布该模块,而您在ng-controller-div中缺少一个结尾
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.2.x" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js" data-semver="1.2.17"></script>
</head>
<body ng-app="myapp">
<div ng-controller="MyController" >
{{1+1}}
<div>Filtered: {{myData.text | myFilter:2:5}}</div>
</div>
<script>
var app = angular.module("myapp", []);
app.controller("MyController", function($scope) {
$scope.myData = {};
$scope.myData.text = "balsalaskdlas";
});
app.filter('myFilter', function() {
return function(stringValue, startIndex, endIndex) {
return stringValue.substring(parseInt(startIndex), parseInt(endIndex));
};
});
</script>
</body>
</html>