2016-03-08 65 views
7

让我们定义的范围简单的布尔:如何在Angular表达式中获得一个范围变量的类型?

var mymodal = angular.module('mymodal', []); 

mymodal.controller('MainCtrl', function ($scope) { 
    $scope.b = false; 
}); 

我怎样才能在表达式中的变量的类型? typeOfObject.prototype.Tostring.call不起作用。

<div ng-controller="MainCtrl" class="container"> 
     <div> 
      {{ b }} 
      {{ typeOf(b) }} 
      {{ Object.prototype.toString.call(b) }} 
     </div> 
</div> 

这里的jsfiddle:http://jsfiddle.net/g8Ld80x3/2/

回答

10

我认为最好的方法是创建一个自定义过滤器,当你希望你的表达中,可以检查this链接,用于获取对象使用它。为你的情况下,对象

的键,你可以使用

angular.module('customfilter', []).filter('typeof', function() { 
    return function(obj) { 
    return typeof obj 
    }; 
}); 
+0

好主意!简单而强大。我喜欢。 – Landeeyo

+0

为什么没有内置到Angular中?也许它已经在Angular 2中。虽然看起来不像它http://stackoverflow.com/questions/37511055/how-to-check-type-of-variable-in-ngif-in-angular2。 –

1

尝试在控制器这样的事情(如你想要做的,我不认为是可能直接在表达式):

var mymodal = angular.module('mymodal', []); 
 

 
mymodal.controller('MainCtrl', function ($scope) { 
 
    $scope.b = false; 
 
    $scope.getBType = function(test){ 
 
     return(typeof test); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app='mymodal' ng-controller="MainCtrl" class="container"> 
 
     <div> 
 
      {{ getBType(b) }} 
 
     </div> 
 
</div>

+0

我不想在控制器中执行此操作。我想在表达式中执行typeof。同样在'ng-if'或'ng-show' /'ng-hide'指令中。 – Landeeyo

+0

首先,您必须在控制器中执行该操作,然后将其返回给表达式。 – Ionut

+0

不应该这样比返回字符串而不是调用alert? – Landeeyo

4

你不能这样做,并有充分的理由:Angular expression解析器不允许这样的在模板中。

如果你真的希望能够这样做,我建议明确设定在$rootScope辅助方法,所以它会在您所有的模板可用:

mymodal.run(function($rootScope) { 
    $rootScope.typeOf = function(value) { 
     return typeof value; 
    }; 
}); 

,你甚至可以参考角度自己的实用方法:

mymodal.run(function($rootScope) { 
    ['isArray', 'isDate', 'isDefined', 'isFunction', 'isNumber', 'isObject', 'isString', 'isUndefined'].forEach(function(name) { 
     $rootScope[name] = angular[name]; 
    }); 
}); 

并在模板中使用{{ isArray(arr) }}

+0

这是非常好的答案,非常感谢。但是Zamboney创建过滤器的想法对我来说更好。无论如何 - 再次感谢。 – Landeeyo

+0

其实,在这种情况下,过滤器是一个非常糟糕的主意,至少有两个原因,但是对你来说更好。 – dfsq

+0

有什么缺点? – Landeeyo

0

当您尝试访问特定值的typeof类型和当前代码时,您在view中执行此操作,这对于此类操作来说相当晚。

相反,你可以做一个函数在控制器的范围,只是从那里返回它:

var mymodal = angular.module('mymodal', []); 
 

 
mymodal.controller('MainCtrl', function ($scope) { 
 
    $scope.b = false; 
 
    $scope.getTypeof = function(it){ return typeof(it); }; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app='mymodal' ng-controller="MainCtrl" class="container"> 
 
     <div> 
 
      {{ b }} : {{ getTypeof(b) }} 
 
      
 
     </div> 
 
</div>

0

HTML

<div ng-controller="MainCtrl" class="container"> 
     <div> 
      {{ b }}<br> 
      {{ typeOf(b) }}<br> 
     </div> 
</div> 

JS

var mymodal = angular.module('mymodal', []); 

mymodal.controller('MainCtrl', function ($scope) { 
    $scope.b = false; 
    $scope.typeOf = function (v){ return (typeof v)}; 
}); 

结果

false 
boolean 
2

只是为了显示应用到我的示例代码Zamboney的回答是:

控制器:

angular.module('customfilter', []).filter('getType', function() { 
    return function(obj) { 
    return typeof obj 
    }; 
}); 

var mymodal = angular.module('mymodal', ['customfilter']); 

mymodal.controller('MainCtrl', function ($scope) { 
    $scope.b = false; 
}); 

查看:

<div ng-controller="MainCtrl" class="container"> 
    <div> 
    {{ b }} 
    {{ b | getType }} 
    <div ng-if="(b | getType) == 'number'"> 
     It's a number 
    </div> 
    <div ng-if="(b | getType) == 'boolean'"> 
     It's a boolean 
    </div> 
    </div> 
</div> 

和小提琴:http://jsfiddle.net/g8Ld80x3/5/

相关问题