ng-model for `<input type=“file”/>` (with directive DEMO)

后端 未结 12 2274
北恋
北恋 2020-11-21 06:40

I tried to use ng-model on input tag with type file:


But after selecting a file, in

12条回答
  •  心在旅途
    2020-11-21 06:49

    This is an addendum to @endy-tjahjono's solution.

    I ended up not being able to get the value of uploadme from the scope. Even though uploadme in the HTML was visibly updated by the directive, I could still not access its value by $scope.uploadme. I was able to set its value from the scope, though. Mysterious, right..?

    As it turned out, a child scope was created by the directive, and the child scope had its own uploadme.

    The solution was to use an object rather than a primitive to hold the value of uploadme.

    In the controller I have:

    $scope.uploadme = {};
    $scope.uploadme.src = "";
    

    and in the HTML:

     
     
    

    There are no changes to the directive.

    Now, it all works like expected. I can grab the value of uploadme.src from my controller using $scope.uploadme.

提交回复
热议问题