Javascript 为什么ng在组件模板中单击“不点火”?
我正在为我的一个项目学习Angular,但我在第一步上遇到了困难 具体地说,我可以通过组件和适当的模板获得要显示的项目列表,但我不知道如何使用组件模型触发ng click事件。许多类似的问题已经得到了回答,但我遵循了许多更正和建议,没有取得进展,需要一些建议Javascript 为什么ng在组件模板中单击“不点火”?,javascript,html,angularjs,angularjs-directive,Javascript,Html,Angularjs,Angularjs Directive,我正在为我的一个项目学习Angular,但我在第一步上遇到了困难 具体地说,我可以通过组件和适当的模板获得要显示的项目列表,但我不知道如何使用组件模型触发ng click事件。许多类似的问题已经得到了回答,但我遵循了许多更正和建议,没有取得进展,需要一些建议 file: customerList.js function CustomerListController($scope, $element, $attrs, $http) { this.customerList = [
file: customerList.js
function CustomerListController($scope, $element, $attrs, $http) {
this.customerList = [
{ name: 'Arya' },
{ name: 'No One' },
];
this.yell = function(customer) {
console.log("customer customer, we've got a click");
};
}
angular.module('myApp').component('customerList', {
templateUrl: 'customerList.html',
controller: CustomerListController,
});
及其模板:
file: customerList.html
<div class="customer"
ng-repeat="customer in $ctrl.customerList"
customer="customer"
ng-click="$ctrl.yell(customer);">
Welcome home, {{customer.name}}!
</div>
文件:customerList.html
欢迎回家,{{customer.name}!
即使我设置了ng click=“console.log('click detected');”
,也没有控制台日志
我相信这是足够的信息来诊断,但请让我知道,如果你需要更多
谢谢 首先,
console.log
不会直接在角度表达式中工作。不能在表达式中直接使用窗口函数
其次,我建议使用
controllerAs
语法,因为这是一种较新的学校做事方式。尝试使用ng-click()表达式中的controllerAs别名访问控制器。感谢提供窗口函数提示!我读到,$ctrl是controllerAs的默认值。将测试并报告。我无法通过别名触发控制器的yell方法。但是,我正在页面上创建一个列表,因此我确实可以访问controller.customerList数组。这可能是一个有约束力的问题吗?就我所知,应该有效:谢谢GBa。你说得对,这应该奏效。我不知道我遇到的问题的根源是什么,但事实证明我实际上是在记录日志,即使它没有出现在我的控制台中。出于某种原因,我输入控制台的任何内容(即使是手动输入)都会立即消失。具有相同代码的新选项卡没有重现此错误,因此我不确定发生了什么。我发现,我通过推到customerList并看到页面的更新来触发这些操作,并且我的console.log语句被完全忽略。这是我现在的问题。我尝试过使用$log及其各种方法,通过$logProvider启用调试,并通过$log.debug发送消息。我的angular应用程序似乎丢失了对窗口的引用?