admin管理员组文章数量:1026154
So I have a variable which could be of any type. By type I mean it could be a string, or an object. I'm trying to run different code depending on the type.
My variable is named range
, it's defined in the JavaScript. What I've tried is. vars
is an object which I'm looping through.
<tr ng-repeat="(key, range) in vars">
<td ng-switch="typeof range">
<span ng-switch-when="string">It's a string</span>
<span ng-switch-when="object">It's an object</span>
<span ng-switch-default>It's something else</span>
</td>
</tr>
but this will throw an invalid expression error. What's the best way to do this?
So I have a variable which could be of any type. By type I mean it could be a string, or an object. I'm trying to run different code depending on the type.
My variable is named range
, it's defined in the JavaScript. What I've tried is. vars
is an object which I'm looping through.
<tr ng-repeat="(key, range) in vars">
<td ng-switch="typeof range">
<span ng-switch-when="string">It's a string</span>
<span ng-switch-when="object">It's an object</span>
<span ng-switch-default>It's something else</span>
</td>
</tr>
but this will throw an invalid expression error. What's the best way to do this?
Share Improve this question edited Nov 20, 2015 at 8:09 Praveen Prasannan 7,13310 gold badges52 silver badges71 bronze badges asked Oct 24, 2015 at 16:39 DowngoatDowngoat 14.4k7 gold badges47 silver badges72 bronze badges 2-
In Controller
$scope.type = typeof $scope.range;
and in view<td ng-switch="type">
. Let me know if this helps. – Tushar Commented Oct 24, 2015 at 16:41 - @Tushar problem is this is in an ng-repeat so I don't think I can do that. Let me clarify – Downgoat Commented Oct 24, 2015 at 16:42
2 Answers
Reset to default 5try this... I also added detection for different types of variables: string
, array
, object
, number
, null
, and undefined
.
"use strict";
angular.module('myApp', [])
.controller("myController", function() {
this.vars = {
key1: "String",
key2: "String2",
key3: 42,
key4: {
name: "Mr. Anderson"
},
key5: [1, 2, 3, 4, 5],
key6: null,
key7: undefined
};
this.getTypeOf = function(value) {
if(value === null) {
return "null";
}
if(Array.isArray(value)) {
return "array";
}
return typeof value;
};
});
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<table ng-app="myApp" ng-controller="myController as ctrl">
<tr ng-repeat="(key, range) in ctrl.vars">
<td ng-switch="ctrl.getTypeOf(range)">
<div ng-switch-when="string">It's a string</div>
<div ng-switch-when="object">It's an object</div>
<div ng-switch-when="number">It's a number</div>
<div ng-switch-when="array">It's an array</div>
<div ng-switch-when="null">It's null</div>
<div ng-switch-when="undefined">It's undefined</div>
<div ng-switch-default>It's something else</div>
</td>
</tr>
</table>
Controller:
$scope.checkType = function(obj){
return typeof obj;
};
Html:
<tr ng-repeat="(key, range) in vars"
<td ng-switch="checkType(range)">
<span ng-switch-when="string">It's a string</span>
<span ng-switch-when="object">It's an object</span>
<span ng-switch-default>It's something else</span>
</td>
</tr>
So I have a variable which could be of any type. By type I mean it could be a string, or an object. I'm trying to run different code depending on the type.
My variable is named range
, it's defined in the JavaScript. What I've tried is. vars
is an object which I'm looping through.
<tr ng-repeat="(key, range) in vars">
<td ng-switch="typeof range">
<span ng-switch-when="string">It's a string</span>
<span ng-switch-when="object">It's an object</span>
<span ng-switch-default>It's something else</span>
</td>
</tr>
but this will throw an invalid expression error. What's the best way to do this?
So I have a variable which could be of any type. By type I mean it could be a string, or an object. I'm trying to run different code depending on the type.
My variable is named range
, it's defined in the JavaScript. What I've tried is. vars
is an object which I'm looping through.
<tr ng-repeat="(key, range) in vars">
<td ng-switch="typeof range">
<span ng-switch-when="string">It's a string</span>
<span ng-switch-when="object">It's an object</span>
<span ng-switch-default>It's something else</span>
</td>
</tr>
but this will throw an invalid expression error. What's the best way to do this?
Share Improve this question edited Nov 20, 2015 at 8:09 Praveen Prasannan 7,13310 gold badges52 silver badges71 bronze badges asked Oct 24, 2015 at 16:39 DowngoatDowngoat 14.4k7 gold badges47 silver badges72 bronze badges 2-
In Controller
$scope.type = typeof $scope.range;
and in view<td ng-switch="type">
. Let me know if this helps. – Tushar Commented Oct 24, 2015 at 16:41 - @Tushar problem is this is in an ng-repeat so I don't think I can do that. Let me clarify – Downgoat Commented Oct 24, 2015 at 16:42
2 Answers
Reset to default 5try this... I also added detection for different types of variables: string
, array
, object
, number
, null
, and undefined
.
"use strict";
angular.module('myApp', [])
.controller("myController", function() {
this.vars = {
key1: "String",
key2: "String2",
key3: 42,
key4: {
name: "Mr. Anderson"
},
key5: [1, 2, 3, 4, 5],
key6: null,
key7: undefined
};
this.getTypeOf = function(value) {
if(value === null) {
return "null";
}
if(Array.isArray(value)) {
return "array";
}
return typeof value;
};
});
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<table ng-app="myApp" ng-controller="myController as ctrl">
<tr ng-repeat="(key, range) in ctrl.vars">
<td ng-switch="ctrl.getTypeOf(range)">
<div ng-switch-when="string">It's a string</div>
<div ng-switch-when="object">It's an object</div>
<div ng-switch-when="number">It's a number</div>
<div ng-switch-when="array">It's an array</div>
<div ng-switch-when="null">It's null</div>
<div ng-switch-when="undefined">It's undefined</div>
<div ng-switch-default>It's something else</div>
</td>
</tr>
</table>
Controller:
$scope.checkType = function(obj){
return typeof obj;
};
Html:
<tr ng-repeat="(key, range) in vars"
<td ng-switch="checkType(range)">
<span ng-switch-when="string">It's a string</span>
<span ng-switch-when="object">It's an object</span>
<span ng-switch-default>It's something else</span>
</td>
</tr>
本文标签: javascriptngswitch with typeof expressionStack Overflow
版权声明:本文标题:javascript - ng-switch with typeof expression - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745617370a2159371.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论