javascript - 带条形码生成器的动态网格 ANGULAR

标签 javascript jquery angularjs barcode

我正在学习 Angular。 我正在制作一个应用程序:用户从一个 html 选择中进行选择,然后填写两个输入字段。此后,用户按“更新”,条形码脚本会生成带有 3 个参数的代码图像:第一个选择和第二个输入。 (这三个之间用一些空格分隔)。到目前为止,没有问题。

我添加了添加新表单的按钮,并且 json 数组正确保存了输入。我想为每个编译的表单生成一个条形码。我能怎么做? 这是我正在做的事情的一个简单示例:

http://plnkr.co/edit/hxZb6g9tkwN0zpRmOMjw?p=preview

在html末尾可以找到条形码的脚本:

<div class="ean">
        <img id="barcodeImage" style="border: solid 1px black;"/>
    <script type="text/javascript">
        function updateBarcode() 
        {
                var barcode = new bytescoutbarcode128();
                var space= "  ";
            var value = document.getElementById("barcodeValue").value;
            var value1 = document.getElementById("barcodeValue1").value;
            var value2 = document.getElementById("barcodeValue2").value;

            barcode.valueSet(value + space + value1 + space + value2);
            barcode.setMargins(5, 5, 5, 5);
            barcode.setBarWidth(2);

            var width = barcode.getMinWidth();

            barcode.setSize(width, 100);

            var barcodeImage = document.getElementById('barcodeImage');
            barcodeImage.src = barcode.exportToBase64(width, 100, 0);
        }
    </script>

最佳答案

您应该创建 directive (另请查看此处 - http://www.ng-newsletter.com/posts/directives.htmlhttp://www.sitepoint.com/practical-guide-angularjs )生成条形码并将此指令放入模板中的 ng-repeat 循环中:

app.directive('barcode', function(){
  return{
    restrict: 'AE',
    template: '<img id="barcodeImage" style="border: solid 1px black;" src="{{src}}"/>',
    scope: {
      food: '='
    },
    link: function($scope){
      $scope.$watch('food', function(food){
        console.log($scope.food);
        var barcode = new bytescoutbarcode128();
        var space= "  ";

            barcode.valueSet([$scope.food.selectproduct, $scope.food.Quantity1, $scope.food.Quantity2].join(space));
            barcode.setMargins(5, 5, 5, 5);
            barcode.setBarWidth(2);

            var width = barcode.getMinWidth();

            barcode.setSize(width, 100);

            $scope.src = barcode.exportToBase64(width, 100, 0);
      }, true);
    }
  }
});

http://plnkr.co/edit/z2nXgXyGi6LhSHth8ZNi?p=preview

关于javascript - 带条形码生成器的动态网格 ANGULAR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33216312/

相关文章:

Jquery Chart 位于文本之上而不是之下 - 如何修复?

用于 xml ajax 调用的 Javascript 到 jQuery 转换

angularjs - ng-init 有条件地赋值

javascript - Angular : factory and controller

javascript - 为什么动画速度参数不带我的变量 jQuery

javascript - DOM直接在javascript中修改部分textnode的样式

javascript - 将函数分配给 jQuery 中的单个字母

javascript - 如何从复选框中获取 id 值?

angularjs - AngularJS 中的搜索引擎优化

Javascript 正则表达式匹配 Twitter 用户名,但不包括冒号