加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular.identity

发布时间:2020-12-17 10:06:30 所属栏目:安全 来源:网络整理
导读:解释:http://stackoverflow.com/questions/15421502/is-there-any-good-example-of-use-cases-for-angular-identity 看ng文档有个奇怪的方法angular.identity。 好像就是你传什么就返回什么。 官方文档给的例子是: function transformer(transformationFn,

解释:http://stackoverflow.com/questions/15421502/is-there-any-good-example-of-use-cases-for-angular-identity

看ng文档有个奇怪的方法angular.identity。

好像就是你传什么就返回什么。

官方文档给的例子是:

function transformer(transformationFn,value) { return (transformationFn || angular.identity)(value); };

实际上等价于

function transformer(transformationFn,value) { if(transformationFn){ return transformationFn(value); }else{ return angular.identity(value); } };

运用场景

// 有下面方法,返回一个数的平方
$scope.square = function(n) {  return n * n }; //把这个方法作为参数使用
 $scope.givemeResult = function(fn,val) { return (fn || angular.identity)(val); }; $scope.initVal = 5; $scope.squareResult = $scope.givemeResult($scope.square,$scope.initVal);

返回25,如果

$scope.squareResult = $scope.givemeResult($scope.square2,$scope.initVal);

因为$scope.square2根本不存在,所以返回5

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读