getting typeof from angularjs $parse
So I understand that $parse creates a function from a string, like $parse('name') will return the name property from a given object. My question is if it can somehow also return the type of said property, something like:
var getType = $parse('typeof name');
var test = getType({name: 'Name1'}); //should be 'string'
angularjs
add a comment |
So I understand that $parse creates a function from a string, like $parse('name') will return the name property from a given object. My question is if it can somehow also return the type of said property, something like:
var getType = $parse('typeof name');
var test = getType({name: 'Name1'}); //should be 'string'
angularjs
For the limitations of expressions that can be parsed read AngularJS Developer Guide - AngularJS Expressions vs. JavaScript Expressions.
– georgeawg
Nov 20 '18 at 12:45
add a comment |
So I understand that $parse creates a function from a string, like $parse('name') will return the name property from a given object. My question is if it can somehow also return the type of said property, something like:
var getType = $parse('typeof name');
var test = getType({name: 'Name1'}); //should be 'string'
angularjs
So I understand that $parse creates a function from a string, like $parse('name') will return the name property from a given object. My question is if it can somehow also return the type of said property, something like:
var getType = $parse('typeof name');
var test = getType({name: 'Name1'}); //should be 'string'
angularjs
angularjs
edited Nov 20 '18 at 11:09
asked Nov 20 '18 at 10:09
Vladimir Moldovan
206
206
For the limitations of expressions that can be parsed read AngularJS Developer Guide - AngularJS Expressions vs. JavaScript Expressions.
– georgeawg
Nov 20 '18 at 12:45
add a comment |
For the limitations of expressions that can be parsed read AngularJS Developer Guide - AngularJS Expressions vs. JavaScript Expressions.
– georgeawg
Nov 20 '18 at 12:45
For the limitations of expressions that can be parsed read AngularJS Developer Guide - AngularJS Expressions vs. JavaScript Expressions.
– georgeawg
Nov 20 '18 at 12:45
For the limitations of expressions that can be parsed read AngularJS Developer Guide - AngularJS Expressions vs. JavaScript Expressions.
– georgeawg
Nov 20 '18 at 12:45
add a comment |
2 Answers
2
active
oldest
votes
The $parse service does not evaluate the JavaScript typeof
operator. If you wish to use typeof
in an AngularJS expression, create a typeof
function and add it to scope.
angular.module("app",)
.run(function($parse) {
var getType = $parse("typeof(name)");
var typeofFn = x => typeof x;
var test = getType({name: "Name1", typeof: typeofFn});
console.log(test); //prints "string"
})
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app">
</body>
For the limitations of expressions that can be parsed read AngularJS Developer Guide - AngularJS Expressions vs. JavaScript Expressions.
If you want to run more complex JavaScript code, you should make it a controller method and call the method from your view.
add a comment |
Check this example $pase give you a setter/getter option.
And then use typeof
(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof) to know what type is the value the getter is using.
Using the getter you get a string in this case.
var Controller = function($parse) {
var vm = this
var getter = $parse('user.name')
var setter = getter.assign
var context = {
user: {
name: 'AngularJS'
}
};
setter(context, 'newValue')
var test = getter(context)
if (typeof test == "boolean") {
console.log("boolean logic")
} else if (typeof test == "string") {
console.log("string logic")
} else if (typeof test == "number") {
console.log("number logic")
} else if (typeof test == "undefined") {
console.log("undefined logic")
}
// IT is a string
vm.test = "typeof= " + typeof test
};
Controller.$inject = ['$parse']
angular
.module('app', )
.controller('Controller', Controller);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="Controller as vm">
{{vm.test}}
</div>
I'm asking if I can get the type within the $parse function. I realize I can get it from the result of the function, but that doesn't help me, my string to parse is more complex and i need to do different operations inside it based on the property type.
– Vladimir Moldovan
Nov 20 '18 at 11:36
Check now the example you can add your logic for any case you want
– ppollono
Nov 20 '18 at 11:52
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390642%2fgetting-typeof-from-angularjs-parse%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The $parse service does not evaluate the JavaScript typeof
operator. If you wish to use typeof
in an AngularJS expression, create a typeof
function and add it to scope.
angular.module("app",)
.run(function($parse) {
var getType = $parse("typeof(name)");
var typeofFn = x => typeof x;
var test = getType({name: "Name1", typeof: typeofFn});
console.log(test); //prints "string"
})
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app">
</body>
For the limitations of expressions that can be parsed read AngularJS Developer Guide - AngularJS Expressions vs. JavaScript Expressions.
If you want to run more complex JavaScript code, you should make it a controller method and call the method from your view.
add a comment |
The $parse service does not evaluate the JavaScript typeof
operator. If you wish to use typeof
in an AngularJS expression, create a typeof
function and add it to scope.
angular.module("app",)
.run(function($parse) {
var getType = $parse("typeof(name)");
var typeofFn = x => typeof x;
var test = getType({name: "Name1", typeof: typeofFn});
console.log(test); //prints "string"
})
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app">
</body>
For the limitations of expressions that can be parsed read AngularJS Developer Guide - AngularJS Expressions vs. JavaScript Expressions.
If you want to run more complex JavaScript code, you should make it a controller method and call the method from your view.
add a comment |
The $parse service does not evaluate the JavaScript typeof
operator. If you wish to use typeof
in an AngularJS expression, create a typeof
function and add it to scope.
angular.module("app",)
.run(function($parse) {
var getType = $parse("typeof(name)");
var typeofFn = x => typeof x;
var test = getType({name: "Name1", typeof: typeofFn});
console.log(test); //prints "string"
})
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app">
</body>
For the limitations of expressions that can be parsed read AngularJS Developer Guide - AngularJS Expressions vs. JavaScript Expressions.
If you want to run more complex JavaScript code, you should make it a controller method and call the method from your view.
The $parse service does not evaluate the JavaScript typeof
operator. If you wish to use typeof
in an AngularJS expression, create a typeof
function and add it to scope.
angular.module("app",)
.run(function($parse) {
var getType = $parse("typeof(name)");
var typeofFn = x => typeof x;
var test = getType({name: "Name1", typeof: typeofFn});
console.log(test); //prints "string"
})
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app">
</body>
For the limitations of expressions that can be parsed read AngularJS Developer Guide - AngularJS Expressions vs. JavaScript Expressions.
If you want to run more complex JavaScript code, you should make it a controller method and call the method from your view.
angular.module("app",)
.run(function($parse) {
var getType = $parse("typeof(name)");
var typeofFn = x => typeof x;
var test = getType({name: "Name1", typeof: typeofFn});
console.log(test); //prints "string"
})
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app">
</body>
angular.module("app",)
.run(function($parse) {
var getType = $parse("typeof(name)");
var typeofFn = x => typeof x;
var test = getType({name: "Name1", typeof: typeofFn});
console.log(test); //prints "string"
})
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app">
</body>
answered Nov 20 '18 at 13:02
georgeawg
32.7k104967
32.7k104967
add a comment |
add a comment |
Check this example $pase give you a setter/getter option.
And then use typeof
(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof) to know what type is the value the getter is using.
Using the getter you get a string in this case.
var Controller = function($parse) {
var vm = this
var getter = $parse('user.name')
var setter = getter.assign
var context = {
user: {
name: 'AngularJS'
}
};
setter(context, 'newValue')
var test = getter(context)
if (typeof test == "boolean") {
console.log("boolean logic")
} else if (typeof test == "string") {
console.log("string logic")
} else if (typeof test == "number") {
console.log("number logic")
} else if (typeof test == "undefined") {
console.log("undefined logic")
}
// IT is a string
vm.test = "typeof= " + typeof test
};
Controller.$inject = ['$parse']
angular
.module('app', )
.controller('Controller', Controller);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="Controller as vm">
{{vm.test}}
</div>
I'm asking if I can get the type within the $parse function. I realize I can get it from the result of the function, but that doesn't help me, my string to parse is more complex and i need to do different operations inside it based on the property type.
– Vladimir Moldovan
Nov 20 '18 at 11:36
Check now the example you can add your logic for any case you want
– ppollono
Nov 20 '18 at 11:52
add a comment |
Check this example $pase give you a setter/getter option.
And then use typeof
(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof) to know what type is the value the getter is using.
Using the getter you get a string in this case.
var Controller = function($parse) {
var vm = this
var getter = $parse('user.name')
var setter = getter.assign
var context = {
user: {
name: 'AngularJS'
}
};
setter(context, 'newValue')
var test = getter(context)
if (typeof test == "boolean") {
console.log("boolean logic")
} else if (typeof test == "string") {
console.log("string logic")
} else if (typeof test == "number") {
console.log("number logic")
} else if (typeof test == "undefined") {
console.log("undefined logic")
}
// IT is a string
vm.test = "typeof= " + typeof test
};
Controller.$inject = ['$parse']
angular
.module('app', )
.controller('Controller', Controller);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="Controller as vm">
{{vm.test}}
</div>
I'm asking if I can get the type within the $parse function. I realize I can get it from the result of the function, but that doesn't help me, my string to parse is more complex and i need to do different operations inside it based on the property type.
– Vladimir Moldovan
Nov 20 '18 at 11:36
Check now the example you can add your logic for any case you want
– ppollono
Nov 20 '18 at 11:52
add a comment |
Check this example $pase give you a setter/getter option.
And then use typeof
(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof) to know what type is the value the getter is using.
Using the getter you get a string in this case.
var Controller = function($parse) {
var vm = this
var getter = $parse('user.name')
var setter = getter.assign
var context = {
user: {
name: 'AngularJS'
}
};
setter(context, 'newValue')
var test = getter(context)
if (typeof test == "boolean") {
console.log("boolean logic")
} else if (typeof test == "string") {
console.log("string logic")
} else if (typeof test == "number") {
console.log("number logic")
} else if (typeof test == "undefined") {
console.log("undefined logic")
}
// IT is a string
vm.test = "typeof= " + typeof test
};
Controller.$inject = ['$parse']
angular
.module('app', )
.controller('Controller', Controller);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="Controller as vm">
{{vm.test}}
</div>
Check this example $pase give you a setter/getter option.
And then use typeof
(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof) to know what type is the value the getter is using.
Using the getter you get a string in this case.
var Controller = function($parse) {
var vm = this
var getter = $parse('user.name')
var setter = getter.assign
var context = {
user: {
name: 'AngularJS'
}
};
setter(context, 'newValue')
var test = getter(context)
if (typeof test == "boolean") {
console.log("boolean logic")
} else if (typeof test == "string") {
console.log("string logic")
} else if (typeof test == "number") {
console.log("number logic")
} else if (typeof test == "undefined") {
console.log("undefined logic")
}
// IT is a string
vm.test = "typeof= " + typeof test
};
Controller.$inject = ['$parse']
angular
.module('app', )
.controller('Controller', Controller);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="Controller as vm">
{{vm.test}}
</div>
var Controller = function($parse) {
var vm = this
var getter = $parse('user.name')
var setter = getter.assign
var context = {
user: {
name: 'AngularJS'
}
};
setter(context, 'newValue')
var test = getter(context)
if (typeof test == "boolean") {
console.log("boolean logic")
} else if (typeof test == "string") {
console.log("string logic")
} else if (typeof test == "number") {
console.log("number logic")
} else if (typeof test == "undefined") {
console.log("undefined logic")
}
// IT is a string
vm.test = "typeof= " + typeof test
};
Controller.$inject = ['$parse']
angular
.module('app', )
.controller('Controller', Controller);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="Controller as vm">
{{vm.test}}
</div>
var Controller = function($parse) {
var vm = this
var getter = $parse('user.name')
var setter = getter.assign
var context = {
user: {
name: 'AngularJS'
}
};
setter(context, 'newValue')
var test = getter(context)
if (typeof test == "boolean") {
console.log("boolean logic")
} else if (typeof test == "string") {
console.log("string logic")
} else if (typeof test == "number") {
console.log("number logic")
} else if (typeof test == "undefined") {
console.log("undefined logic")
}
// IT is a string
vm.test = "typeof= " + typeof test
};
Controller.$inject = ['$parse']
angular
.module('app', )
.controller('Controller', Controller);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="Controller as vm">
{{vm.test}}
</div>
edited Nov 20 '18 at 11:55
answered Nov 20 '18 at 11:21
ppollono
2,43311526
2,43311526
I'm asking if I can get the type within the $parse function. I realize I can get it from the result of the function, but that doesn't help me, my string to parse is more complex and i need to do different operations inside it based on the property type.
– Vladimir Moldovan
Nov 20 '18 at 11:36
Check now the example you can add your logic for any case you want
– ppollono
Nov 20 '18 at 11:52
add a comment |
I'm asking if I can get the type within the $parse function. I realize I can get it from the result of the function, but that doesn't help me, my string to parse is more complex and i need to do different operations inside it based on the property type.
– Vladimir Moldovan
Nov 20 '18 at 11:36
Check now the example you can add your logic for any case you want
– ppollono
Nov 20 '18 at 11:52
I'm asking if I can get the type within the $parse function. I realize I can get it from the result of the function, but that doesn't help me, my string to parse is more complex and i need to do different operations inside it based on the property type.
– Vladimir Moldovan
Nov 20 '18 at 11:36
I'm asking if I can get the type within the $parse function. I realize I can get it from the result of the function, but that doesn't help me, my string to parse is more complex and i need to do different operations inside it based on the property type.
– Vladimir Moldovan
Nov 20 '18 at 11:36
Check now the example you can add your logic for any case you want
– ppollono
Nov 20 '18 at 11:52
Check now the example you can add your logic for any case you want
– ppollono
Nov 20 '18 at 11:52
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390642%2fgetting-typeof-from-angularjs-parse%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
For the limitations of expressions that can be parsed read AngularJS Developer Guide - AngularJS Expressions vs. JavaScript Expressions.
– georgeawg
Nov 20 '18 at 12:45