簡體   English   中英

控制器鏈接中的AngularJs調用函數

[英]AngularJs calling function in link from controller

我有一個父控制器,我想從控制器的鏈接中調用一個函數,但是當嘗試訪問它時,

TypeError: undefined is not a function

我的控制器:

scope.test = function(m)
{
 linkfunction(m);
}

我的指令: ..........鏈接:function(scope,element,attrs){

 linkfunction = function (n){
  ........somethings........
  }

  ..........
  }

我如何在指令中的鏈接中調用函數?

1)使用隔離范圍

的HTML

<div test-directive callback-fun="testFunction()"></div>

控制器:

$scope.testFunction = function(){
  console.log("test")
}

指示:

.directive('testDirective', function() {
     return {
         scope: {
            callbackFun: '&'
         },
         link: function(scope, element, attrs) 
         {
            scope.callbackFun();
         }
     }
 })

2)不使用隔離范圍

HTML:

<div test-directive></div>

控制器:

$scope.testFunction = function(){
  console.log("test")
}

指示:

.directive('testDirective', function() {
     return {
         link: function(scope, element, attrs) 
         {
            scope.testFunction();
         }
     }
 })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM