【问题标题】:adding dynamic image to table column does not work in angularjs向表格列添加动态图像在angularjs中不起作用
【发布时间】:2018-04-11 14:52:09
【问题描述】:

我是 angulajs 的新手,我正在尝试在用户选择时将图像动态添加到列中。 这是下面的代码,它不起作用。 .cshtml 文件中的代码 ---

<table>
        <tr>
            <td>
                <img ng-src="{{path}}" />
                <input type="file" ng-model="path" />
            </td>
        </tr>
</table>

angularjs 文件中的代码

$scope.path = "";

我遇到了 1 个示例,但它是用 jquery 编写的,而这正是我需要的。 http://jsfiddle.net/dwebexperts/4FGg8/1/

谢谢

【问题讨论】:

    标签: angularjs image dynamic


    【解决方案1】:

    对于您的示例,它看起来很安静。我建议不要进入任何插件。 Check this plukr

      $scope.imageUpload = function(event){
       var files = event.target.files; 
       var reader = new FileReader();
       reader.onload = $scope.imageIsLoaded; 
       reader.readAsDataURL(files[0]);
      }
    
      $scope.imageIsLoaded = function(e){
       $scope.$apply(function() {
          $scope.path = e.target.result;
       });
      }
    

    在你的html

    <input type="file" ng-model="path" onchange="angular.element(this).scope().imageUpload(event)"/>
    

    注意:ng-change 不支持类型 file

    【讨论】:

    • 我使用 VS2015 作为编辑器,它不起作用..出现错误
    • 什么错误?你检查过我的 plnkr 吗?是你想要的吗?用VS2015 错误更新问题
    • 为什么和VS2015有关?
    • 不可能是IDE相关问题。分享错误图片
    • @Shashank Vivek - 这正是我想要的。
    猜你喜欢
    • 2017-06-21
    • 2021-02-19
    • 1970-01-01
    • 1970-01-01
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多