AngularJS 指令元素方法 bind-TypeError: 不能使用“ in”运算符在1中搜索“ function Name”

这是主模板的控制器:

app.controller('OverviewCtrl', ['$scope', '$location', '$routeParams', 'websiteService', 'helperService', function($scope, $location, $routeParams, websiteService, helperService) {
...
$scope.editWebsite = function(id) {
$location.path('/websites/edit/' + id);
};
}]);

这是指令:

app.directive('wdaWebsitesOverview', function() {
return {
restrict: 'E',
scope: {
heading: '=',
websites: '=',
editWebsite: '&'
},
templateUrl: 'views/websites-overview.html'
}
});

这是该指令在主模板中的应用方式:

<wda-websites-overview heading="'All websites'" websites="websites" edit-website="editWebsite(id)"></wda-websites-overview>

这是从指令模板(site-overview.html)调用的方法:

<td data-ng-click="editWebsite(website.id)">EDIT</td>

问: 单击编辑时,控制台中出现这个错误:

TypeError: 无法使用“ in”运算符在1中搜索“ edit Site”

有人知道这里发生了什么吗?

35531 次浏览

Since you defined an expression binding (&), you need to explicitly call it with an object literal parameter containing id if you want to bind it in the HTML as edit-website="editWebsite(id)".

Indeed, Angular needs to understand what this id is in your HTML, and since it is not part of your scope, you need to add what are called "locals" to your call by doing:

data-ng-click="editWebsite({id: website.id})"

Or as an alternative:

data-ng-click="onClick(website.id)"

With the controller/link code:

$scope.onClick = function(id) {
// Ad "id" to the locals of "editWebsite"
$scope.editWebsite({id: id});
}

AngularJS includes an explanation of this in its documentation; look for the example involving "close({message: 'closing for now'})" at the following URL:

https://docs.angularjs.org/guide/directive

TL;DR; - You are assuming that the bound function is being passed to the child component, as it would be in React. This is incorrect. In fact, AngularJS is parsing the string template and creating a new function, which then calls the parent function.

This generated function expects to receive an object with keys and values, rather than a plain variable.

Longer Explanation

This happens when you have bound a function using '&', and have tried to call that function from your controller, passing a plain variable rather than an object containing the name of the plain variable. The object keys are needed by the templating engine to work out how to pass values into the bound function.

eg. you have called boundFunction('cats') rather than boundFunction({value: 'cats'})

Worked Example

Say I create a component like this:

const MyComponent = {
bindings: {
onSearch: '&'
},
controller: controller
};

This function (in the parent) looks like this:

onSearch(value) {
// do search
}

In my parent template, I can now do this:

<my-component on-search="onSearch(value)"></my-component>

The binding here will be parsed from the string. You're not actually passing the function. AngularJS is making a function for you which calls the function. The binding created in the template can contain lots of things other than the function call.

AngularJS somehow needs to work out where to get value from, and it does this by receiving an object from the parent.

In myComponent controller, I need to do something like:

handleOnSearch(value) {
if (this.onSearch) {
this.onSearch({value: value})
}
}