gpt4 book ai didi

AngularJS 指令回调

转载 作者:行者123 更新时间:2023-12-03 13:39:23 25 4
gpt4 key购买 nike

我想通过标签上的参数将调用发送回指令,然后在指令内适当时调用该方法。例如,当单击按钮时,调用父 Controller 上的方法。

我有一个简单的plunker它不起作用

.html 文件:

<body ng-controller="ParentController">
<h1> Method Arguments </h1>
<h3> open console to view output</h3>
<hello-world onLoadCallback="myCallback(arg1)"></hello-world>
</body>

javascript文件:
var app = angular.module("app",[]);

function ParentController($scope) {
$scope.myCallback = function(var1){
console.log("myCallback", var1);
}
}
app.directive('helloWorld', function() {
return {
restrict: 'AE',
template: '<h3>Hello World!!</h3>',
scope:{
onLoadCallback: '&'
},
link: function(scope, element, attrs, dateTimeController) {
console.log('linked directive, not calling callback')
scope.onLoadCallback('wtf');

}
};
});

最佳答案

Tricky Tricky Angular,在 HTML 中声明参数时,需要使用蛇形大小写,而不是驼峰形大小写来匹配。

作品:

<hello-world on-load-callback="myCallback(arg1)"></hello-world>

不工作:
<hello-world onLoadCallback="myCallback(arg1)"></hello-world>

关于AngularJS 指令回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24640284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com