I have the directory
component passing a function to the question
component. The question
component is to take an input, and then update the directory
component with this input via the function. When the question
component tries to access this function, there is the JS error: angular.min.js:123 TypeError: Cannot use 'in' operator to search for '$ctrl' in Lee
. What am I doing incorrectly to trigger this error?
https://plnkr.co/edit/hXjhhJcCWPcavp8Z8BRa?p=preview
let directoryTemplate =
'<question on-click="$ctrl.updateLastName()"></question>' +
'<br />' +
'Full name: {{$ctrl.fullName}}';
class directoryController {
constructor() {
this.fullName;
this.firstName = 'Jack';
}
updateLastName(lastName) {
this.fullName = `${this.firstName} ${lastName}`;
}
}
let questionTemplate =
'<input ng-model="$ctrl.input" />' +
'<button ng-click="$ctrl.onClick($ctrl.input)">Submit</button>';
class questionController {
constructor() {
this.input;
}
}
angular
.module('app', [])
.component('directory', {
template: directoryTemplate,
controller: directoryController
})
.component('question', {
template: questionTemplate,
controller: questionController,
bindings: {
onClick: '&'
}
});
Cause of the Error: The in operator can be used only for to check if a property is in an object. This can not be used for search in strings, numbers, or other primitive types. Example 1: in operator can not be used for string search, So the error has occurred. Example 2: in operator can not be used for string search, So the error has occurred.
TypeError: cannot use 'in' operator to search for 'x' in 'y' The JavaScript exception "right-hand side of 'in' should be an object" occurs when the in operator was used to search in strings, or in numbers, or other primitive types. It can only be used to check if a property is in an object.
TypeError: cannot use 'in' operator to search for 'x' in 'y'. The JavaScript exception "right-hand side of 'in' should be an object" occurs when the in operator was used to search in strings, or in numbers, or other primitive types. It can only be used to check if a property is in an object.
This JavaScript exception Cannot use ‘in’ operator to search for ‘X’ in ‘Y’ occurs if in operator is used to search in strings, numbers, or other primitive types. It can not be used other than type-checking.
A similar question has been answered here: AngularJS Directive element method binding - TypeError: Cannot use 'in' operator to search for 'functionName' in 1
In a nutshell, in your template calling the component, you need to pass the signature of your function (including the argument's name):
<question on-click="$ctrl.updateLastName(lastName)">
And in your component you need to call your function with a JSON containing the lastName:
<button ng-click="$ctrl.onClick({lastName: $ctrl.input})">
See the uploaded plunker: https://plnkr.co/edit/iOA29KGEbl6NLubP1cvb?p=preview
By the way you should use ES6 template strings for your multiline templates, like you did for updateLastName method (see https://developers.google.com/web/updates/2015/01/ES6-Template-Strings).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With