Javascript 为什么角函数需要嵌套?
我刚开始学习AngularJS,我看到了一个教程,其中给出了以下示例:Javascript 为什么角函数需要嵌套?,javascript,angularjs,Javascript,Angularjs,我刚开始学习AngularJS,我看到了一个教程,其中给出了以下示例: <!DOCTYPE html> <html> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <body> <ul ng-app="myApp" ng-controller="namesCtrl"> <li n
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<ul ng-app="myApp" ng-controller="namesCtrl">
<li ng-repeat="x in names">
{{x | myFormat}}
</li>
</ul>
<script>
var app = angular.module('myApp', []);
app.filter('myFormat', function() {
return function(x) {
var i, c, txt = "";
for (i = 0; i < x.length; i++) {
c = x[i];
if (i % 2 == 0) {
c = c.toUpperCase();
}
txt += c;
}
return txt;
};
});
app.controller('namesCtrl', function($scope) {
$scope.names = [
'Jani',
'Carl',
'Margareth',
'Hege',
'Joe',
'Gustav',
'Birgit',
'Mary',
'Kai'
];
});
</script>
<p>Make your own filters.</p>
<p>This filter, called "myFormat", will uppercase every other character.</p>
</body>
</html>
-
{{x | myFormat}}
var-app=angular.module('myApp',[]);
app.filter('myFormat',function()){
返回函数(x){
变量i,c,txt=“”;
对于(i=0;i
这个名为“myFormat”的过滤器将每隔一个字符使用大写字母
我想知道为什么函数需要嵌套?为什么我不能写这个:
var app = angular.module('myApp', []);
app.filter('myFormat', function(x) {
var i, c, txt = "";
for (i = 0; i < x.length; i++) {
c = x[i];
if (i % 2 == 0) {
c = c.toUpperCase();
}
txt += c;
}
return txt;
});
var-app=angular.module('myApp',[]);
应用程序过滤器('myFormat',函数(x){
变量i,c,txt=“”;
对于(i=0;i
还有一个问题-在哪里\谁将x传递给函数?我知道大多数时候我都是这样将数据传递给finction-foo(x,y)
-这里在哪里
谢谢 这是故意的<代码>过滤器API具有返回
函数
(过滤逻辑)的函数。基本上,外部可以用来利用角度相关性。内部返回的函数在每个摘要循环中都会被求值
//sample filter
app.filter('upperCase',[ '$window', function($window){ //you could have dependency here
//inner function
return function(x){
return x.toUpperCase();
}
}]);
在上面的x
中,是应用过滤器的值。在您的例子中,{{x | myFormat}
该参数将是x
变量范围值。每当您想在过滤器中传递多个参数时,您可以通过在过滤器名称后面加上“”:
{{x | myFormat: y: z}}