簡體   English   中英

angularjs指令傳遞參數輸出控制器

[英]angularjs directive pass parameter out controller

我將控制器方法發送到指令。 我想像事件一樣獲取參數frpm的偏差。

演示

var app = angular.module("app", []);

app.controller("outCtrl", function($scope){
        $scope.callOut = function(pramFromDirective){
        console.log(pramFromDirective);
    }
})


app.directive("myDir", function(){
  return {
            restrict: "E",
            scope: {
                call: "&"
        },
            controller: function($scope){
            $scope.call({message: 444});
        }
  }
})

我想向outCtrl.callOut()方法發送參數。 但事實並非如此。

您必須在模板中添加與傳遞給$scope.call()的對象的屬性名稱相同的參數。 由於您將其稱為$scope.call({message: 444})請執行以下操作:

<my-dir call="callOut(message)"></my-dir>

您的模板不太正確。 您需要使回調方法綁定中的參數與您在指令中使用的對象鍵的名稱完全匹配。

<div ng-app="app">
  <div ng-controller="outCtrl">
    <my-dir call="callOut(message)"></my-dir>
  </div>
</div>

Angular實際上並沒有直接傳遞參數。 它將對象鍵從指令函數調用參數$scope.call({ message: 444 })映射到模板call="callout(message)"中的方法的參數名稱。

暫無
暫無

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

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