有棱角的从html调用指令中的函数

Angular. call function in directive from html

本文关键字:指令 函数 调用 html      更新时间:2023-09-26

我正在尝试生成一个指令。这样我就可以在我的html页面中使用它,就像下面的片段:

<testview></testview>

现在唯一要做的就是渲染一个类似的链接

<a><span ng-onmousedown='onSelected('"creame'",event)'>{{value}}</span></a>

当我单击span时,我希望指令通过调用onSelected函数来更改$scope.value。但我不能这么说。

全代码样本

function testView() {
    $scope.value =
    {
        name: "dummy"
    }
    testHtml += "<a><span ng-onmousedown='onSelected('"creame'",event)'  >" + $scope.value.name + "</span></a>";
    return {
        restrict: 'AE',
        replace: 'true',
        template: testHtml,
    };
}
app.directive('testview', testView);
function onSelected(input)
{
    $scope.value.name = input;
}

有人知道我该怎么做吗?感谢您抽出时间

试试这个

function testView() {
testHtml += "<a><span ng-onmousedown='onSelected('"creame'")'  >{{value.name }}</span></a>";
    return {
        restrict: 'AE',
        replace: 'true',
        template: testHtml,
    };
}
app.directive('testview', testView);
app.controller('CampaignsOverviewController', Controller);
function Controller ($scope) {
  $scope.onSelected = onSelected;
  $scope.value =
       {
          name: "dummy"
       }
     function onSelected(input)
       {
         $scope.value.name = input;
        }
 }

我认为在执行ngOnMouseDown指令之前,您需要编译模板。