【问题标题】:how to get arguments in directive如何在指令中获取参数
【发布时间】:2018-11-21 09:22:49
【问题描述】:

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

        }
    }
}]);

【问题讨论】:

    标签: javascript angularjs angular-directive


    【解决方案1】:

    试试这个

    <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

    【讨论】:

    • 这个按钮怎么样&lt;button upload-file="selectedDocumentName,loanFolderNumber" &gt;&lt;!-- ng-click="uploadFile(selectedDocumentName,FolderNumber)" --&gt;
    • 我告诉你只将此按钮更改为我建议的按钮。
    • 在你的建议按钮不存在
    • 通话正在进行,但值未到来
    猜你喜欢
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    相关资源
    最近更新 更多