简体   繁体   English

在Angular之外更改时,在ng-click上处理输入值

[英]Process input values on ng-click when they changed outside of Angular

How to pass input values on ng-click when they changed outside of angular. 当输入值在角度范围之外更改时,如何在ng-click上传递输入值。 When I type texts myself all works good but once input got dynamic values, ng-click passes empty form. 当我自己输入文本时,一切正常,但是一旦输入获得动态值,ng-click就会传递空格式。 Here the HTML I'll be using: 我将在这里使用HTML:

<form id="form" action="" style="margin:0;">
    <img src="jCrop/images/imagename.jpg" id="imgcrop"/>
    <input type="text" name="hdnx" id="hdnx" data-ng-model="thumbnail.hdnx" ng-change="alert('test')" />
    <input type="text" name="hdny" id="hdny" data-ng-model="thumbnail.hdny" />
    <input type="text" name="hdnw" id="hdnw" data-ng-model="thumbnail.hdnw" />
    <input type="text" name="hdnh" id="hdnh" data-ng-model="thumbnail.hdnh" />
    <button ng-click="save()">Crop Image & Save Selection</button>
</form>

Here is the AngularJS codes: 这是AngularJS代码:

angular.module('blogAdmin').controller('ThumbnailsController', ["$rootScope", "$scope", "$location", "$http", "$filter", "dataService", function ($rootScope, $scope, $location, $http, $filter, dataService) {
  $scope.thumbnail = {};
  $scope.save = function () {
    if ($scope.thumbnail) {
        console.log($scope.thumbnail);  //empty log when values changes outside of angular
    }
  }
}]);

Through googling I noticed $scope.$apply(); 通过谷歌搜索,我注意到$ scope。$ apply();。 will help me, if so how to use this in above form. 如果有的话,如何在上面的表格中使用它会有所帮助。

UPDATE 1 更新1

Values are changes through jQuery code sitting on HTML page directly:- 值是通过直接位于HTML页面上的jQuery代码进行的更改:-

<script type="text/javascript">
    $(function () {
        $('#imgcrop').Jcrop({
            onSelect: getcroparea,
            aspectRatio: 1  //square selection to crop
        });
    })
    function getcroparea(c) {
        $('#hdnx').val(c.x);
        $('#hdny').val(c.y);
        $('#hdnw').val(c.w);
        $('#hdnh').val(c.h);
        console.log(c.h + " : " + c.w);
        $('#selectedSize').html("Selected region " + c.h + "px : " + c.w + "px");
    };
</script>

UPDATE 1 更新1

"$scope is not defined" when used 使用时“未定义$ scope”

    function getcroparea(c) {
        $('#hdnx').val(c.x);
        $('#hdny').val(c.y);
        $('#hdnw').val(c.w);
        $('#hdnh').val(c.h);
        console.log(c.h + " : " + c.w);
        $('#selectedSize').html("Selected region " + c.h + "px : " + c.w + "px");

        $scope.$apply();
    };

You should wrap both the form and croppable area in a directive. 您应该在指令中包装形式和可裁剪区域。 Here you can access the DOM element that you apply the third-party library to and instead of changing the input text value, change it directly on your scope variable. 在这里,您可以访问将第三方库应用于其的DOM元素,而不是更改输入文本值,而直接在您的作用域变量上对其进行更改。

There maybe easy solution but I solved this using this: 也许有简单的解决方案,但是我使用以下方法解决了这个问题:

<script type="text/javascript">
$(function () {
    $('#imgcrop').Jcrop({
        onSelect: getcroparea,
        aspectRatio: 1  //square selection to crop
    });
})
function getcroparea(c) {
    $('#hdnx').val(c.x);
    $('#hdny').val(c.y);
    $('#hdnw').val(c.w);
    $('#hdnh').val(c.h);
    console.log(c.h + " : " + c.w);
    $('#selectedSize').html("Selected region " + c.h + "px : " + c.w + "px");

    $scope.thumbnail = { "id": $scope.id, "hdnx": c.x, "hdny": c.y, "hdnw": c.w, "hdnh": c.h, "imgcrop": $scope.firstImageSrc };

};
</script>

$scope.thumbnail = .. is new in above code which assigns the scope variable every-time. $ scope.thumbnail = ..是上述代码中的新功能,它每次都会分配作用域变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM