angular.identity

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了angular.identity相关的知识,希望对你有一定的参考价值。

解释: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

以上是关于angular.identity的主要内容,如果未能解决你的问题,请参考以下文章

微信小程序代码片段

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器