admin管理员组文章数量:1204985
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?
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', [])
ponent('directory', {
template: directoryTemplate,
controller: directoryController
})
ponent('question', {
template: questionTemplate,
controller: questionController,
bindings: {
onClick: '&'
}
});
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: '&'
}
});
Share
Improve this question
asked Jan 29, 2018 at 5:32
JonJon
8,51131 gold badges95 silver badges149 bronze badges
1 Answer
Reset to default 24A 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).
本文标签: javascriptAngularJS Error Cannot use 39in39 operator to search for 39ctrl39 inStack Overflow
版权声明:本文标题:javascript - AngularJS Error: Cannot use 'in' operator to search for '$ctrl' in - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738695170a2107333.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论