javascript - 如何在指令中获取参数
问题描述
我有一个按钮,当我单击该功能时,我需要调用一个指令。为此,我编写了以下代码。在创建指令之前有一个 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){
})
}
}
}]);
解决方案
尝试这个
<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"};
});
检查这个小提琴
推荐阅读
- python - PyCall 无法使用 python InitError 的 pipenv 版本:检测到不兼容的`libpython`
- arcgis - 在 ArcGIS 或 QGIS 中添加大量标签是否有快速、有效的方法?
- python - How do I read a non standard csv file into dataframe with python or scala
- c - fread() 读取太多数据
- version-control - Perforce - 恢复我本地工作区中另一个用户打开的文件
- c# - 如何在不加载整个excel文件的情况下读取excel中的标题行(假设第一行是标题)
- typescript - 如何覆盖 TypeScript 中父类中重载的方法?
- ios - 如何获得 MTLTexture 内容的一致深拷贝?
- tomcat - 配置选项的 Tomcat 9 ant 部署问题。为什么?
- ios - 如何在 ViewControllers 之间为不同的 tableViews 提供相同的操作 [swift]