[英]AngularJs call controller function from dynamically created element
我有一個控制器,並希望創建動態元素。當我創建它時,該元素應調用controller函數,但不調用該函數
app.controller('AbnTestController', ['$scope','$timeout',function($scope,$timeout) {
..........................
var tree1;
tree1 = [
{
label: 'test1',
id:'1',
type:'t',
}];
return $scope.addnew= function() {
.........
something
};
在我的指令中,我創建了動態元素;
app.directive('mytree', [
'$timeout', function($timeout) {
return {
restrict: 'E',
controller: function($scope) {
$scope.launch = function (mn) {........something.....}},
template: "<a ng-click='addnew()'>Click</a>",
replace: true,
scope: {
treeData: '=',
onSelect: '&',
initialSelection: '@',
treeControl: '='
}
...........
}}]);
我想從動態創建的元素中調用“ addnew”函數。
每個指令都有一個隔離的范圍。
這意味着指令模板所在的范圍中'addNew'函數不可用。
為此,您可以將以下行添加到指令定義的'scope'屬性中:
addNew: '&'
這會將scope屬性綁定到原始范圍之一(請參閱: https : //docs.angularjs.org/guide/directive )
您的指令應如下所示:
app.directive('mytree', [
'$timeout', function($timeout) {
return {
restrict: 'E',
controller: function($scope) {
$scope.launch = function (mn) {........something.....}},
template: "<a ng-click='addnew()'>Click</a>",
replace: true,
scope: {
treeData: '=',
onSelect: '&',
initialSelection: '@',
treeControl: '=',
addNew: '&'
}
...........
}}]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.