首页 > 解决方案 > 如何在指令中获取参数

问题描述

我有一个按钮,当我单击该功能时,我需要调用一个指令。为此,我编写了以下代码。在创建指令之前有一个 ng-change 函数。我已删除 ng-change 并保留指令如下,

<button  upload-file="selectedDocumentName,loanFolderNumber" ><!-- ng-click="uploadFile(selectedDocumentName,FolderNumber)" -->

我怎样才能selectedDocumentName,FolderNumber在我的指令中接受这些参数。我已经尝试过以下方式,但我没有得到这些值。

指示:-

app.directive('uploadFile',['documentService',function(documentService){
    return {
        restrict: 'AE', 
        scope: {
            selectedDocumentName: '=',
            FolderNumber: '=',
        },controller: function($scope){
            $scope.selectedDocumentName;
        },
        link : function($scope,element,attrs){

            element.on('click',function(e){
            })

        }
    }
}]);

标签: javascriptangularjsangular-directive

解决方案


尝试这个

<button upload-file obj="obj">A</button>

    var myApp = angular.module('myApp',[]);


    myApp.directive('uploadFile', function() {
        return {
            restrict: 'AE', 
            scope: {
                obj1: '='
            },controller: function($scope){
                $scope.obj2 = JSON.parse(JSON.stringify($scope.obj1))
                console.log($scope.obj2.selectedDocumentName);
                $scope.obj2.selectedDocumentName = "DEF";
                 console.log($scope.obj2.selectedDocumentName);
            },
            link : function($scope,element,attrs){

                element.on('click',function(e){
                })

            }
        }
    });


    myApp.controller('MyCtrl', function ($scope) {
       $scope.obj = {selectedDocumentName : "ABC",loanFolderNumber : "DEDF"};
    });

检查这个小提琴


推荐阅读