我刚开始学习AngularJS,我看到了一个教程,给出了以下示例:
;With Cte_1(ID,ProductCode,[Date],PurchQty,CurrentStock)
As
(
SELECT 1001,'AB101','14/12/2016', 9,14 Union All
SELECT 1111,'AB101','01/01/2017', 18,14 Union All
SELECT 1223,'AB101','15/01/2017', 20,14 Union All
SELECT 1233,'BB400','02/01/2017', 50,40 Union All
SELECT 1321,'AB101','31/01/2017', 8,14 Union All
SELECT 1400,'BB400','12/12/2016', 90,40 Union All
SELECT 1456,'CC200','13/03/2017',100,20
),
cte2 AS (
SELECT ProductCode
,[Date]
,CASE
WHEN Sum(PurchQty) OVER (
PARTITION BY ProductCode
,[DATE] ORDER BY ProductCode
) >= CurrentStock
THEN PurchQty
END SumPurchQty
,CurrentStock
FROM Cte_1
)
,Cte3 AS (
SELECT ROW_NUMBER() OVER (
PARTITION BY ProductCode ORDER BY SumPurchQty DESC
) AS SeqOrder
,ProductCode,[DATE],SumPurchQty,CurrentStock
FROM cte2
)
SELECT ProductCode
,[Date]
,SumPurchQty
,CurrentStock
FROM Cte3
WHERE SeqOrder = 1
我想知道 - 为什么函数需要嵌套?为什么我不能写这个:
<!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传递给函数?我知道大部分时间我都会将数据传递到这样的结果 - 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;
});
- 这里在哪里?
谢谢!
答案 0 :(得分:1)
这是设计。 filter
API具有返回function
(过滤逻辑)的函数。基本上可以利用外部来利用角度依赖性。并且在每个摘要周期中评估内部返回函数。
//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}}