如何在指令中获取参数

时间:2018-06-12 04:42:44

标签: javascript angularjs angular-directive

我有一个按钮,当我点击该功能时,我需要调用一个指令。 为此,我编写了以下代码。在创建指令之前,有一个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){
            })

        }
    }
}]);

1 个答案:

答案 0 :(得分:2)

试试这个

<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"};
    });

选中此fiddle