从指令传递值到控制器

时间:2016-12-26 09:39:37

标签: angularjs function controller element directive

我正在创建一个网络应用,我在其中为input type file

创建了一个指令
app.directive('customFileInput', [function () {
        return {
            link: function (scope, element, attrs) {
                element.on('change', function (evt) {
                    var files = evt.target.files;
                    scope.filename = files[0].name
                    console.log(scope.filename);
                });
            }
        }
    }]);

这是我从

获取文件名的指令
<input id="file" type="file" ng-model="mdfile" ng-change="filepath()" class="form-control"  custom-file-input />

此输入字段

现在在我的控制器中我想传递我从指令

获得的文件名
$scope.savefunction = function () {
            var f = document.getElementById('file').files[0],
                r = new FileReader();
            r.onloadend = function (e) {
                $scope.data = e.target.result;
            }
            r.readAsDataURL(f);

            $http.get('/employeeregistration.asmx/sav', {
                params: {
                    data: $scope.data,
                    //i want to pass the value here
                }
            }).then(function (response) {

            })
        }

我如何传递价值

1 个答案:

答案 0 :(得分:1)

scope: false

中使用DDO
return { 
  restrict: “EA”, 
  scope: false, 
  link: function(scope, elem, attr){ 
   // write code here. 
  } 
} 
两个地方的名称filename相同。 example

OR

使用isolated scope

return {
    restrict: "EA",
    scope: {
        filename: "="
    },

isolated example 希望这会有所帮助。