在AngularJS中,Jquery$(this).val()等价于什么?如果更新后输入字段为空,如何将输入字段值设置为零? $(文档).on('change','.botentry',函数(){ if($(this).val()==“”){ $(此).val(0); } });

在AngularJS中,Jquery$(this).val()等价于什么?如果更新后输入字段为空,如何将输入字段值设置为零? $(文档).on('change','.botentry',函数(){ if($(this).val()==“”){ $(此).val(0); } });,jquery,angularjs,Jquery,Angularjs,我的html代码是 <script> $(document).on('change', '.botentry', function () { if ($(this).val() == "") { $(this).val(0); } }); </script> {b.bizgroup} {b.mtmsegment} {{b.family} {{b.series}} {b.formfactor} {{b.m1.value} {{b.m2.v

我的html代码是

<script>
$(document).on('change', '.botentry', function () {
    if ($(this).val() == "") {
        $(this).val(0);
    }
});
</script>

{b.bizgroup}
{b.mtmsegment}
{{b.family}
{{b.series}}
{b.formfactor}
{{b.m1.value}
{{b.m2.value}
{{b.m3.value}
{{b.m1.value+b.m2.value+b.m3.value}

我添加此代码是为了避免在输入框中输入空字段,但它不适用于AngularJs,Jquery值零不与AngularJs ng模型绑定。

在AngularJs中,使用
$scope

您不更新输入,而是更新输入所指向的模型 束缚

例如:

您的html

 <tbody id=" tbody_Buid" ng-if="cstate!=''">
    <tr ng-repeat="b in BottomsUpData | filter:{qtr: cqtr, state: cstate} | filter:searchData | orderBy:['-mtmsegment','bizgroup']" style="text-align:center">
      <td><div>{{b.bizgroup}}</div></td>
      <td><div>{{b.mtmsegment}}</div></td>
      <td><div>{{b.family}}</div></td>
      <td><div>{{b.series}}</div></td>
      <td><div>{{b.formfactor}}</div></td>
      <td><div><span ng-if="!b.freeze.m1 || !b.m1.isEdit">{{b.m1.value}}</span><input class="botentry" ng-if="b.freeze.m1 && b.m1.isEdit" ng-model="b.m1.value" ng-blur="buTotCalc()" type="number" /></div></td>
      <td><div><span ng-if="!b.freeze.m2 || !b.m2.isEdit">{{b.m2.value}}</span><input class="botentry" ng-if="b.freeze.m2 && b.m2.isEdit" ng-model="b.m2.value" ng-blur="buTotCalc()" type="number" /></div></td>
      <td><div><span ng-if="!b.freeze.m3 || !b.m3.isEdit">{{b.m3.value}}</span><input class="botentry" ng-if="b.freeze.m3 && b.m3.isEdit" ng-model="b.m3.value" ng-blur="buTotCalc()" type="number" /></div></td>
      <td><div>{{b.m1.value+b.m2.value+b.m3.value}}</div></td>
    </tr>
  </tbody>
<div ng-controller="MyCtrl">
    <input type='text' ng-model='input' ng-click="myFunction('some value')">
</div>
如下所示创建自定义指令(选中空),并将此指令添加到HTML代码中,如下所示

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
   $scope.input = 1;
   $scope.myFunction = function(value) {
        $scope.input = value;
  }
}

您只需将
$event
作为参数传递给
buTotCalc
函数,然后使用
$event.target.value
获取相关元素的值

myApp.directive('checkEmpty', function () { 
    return { 
        restrict: 'A', 
        link: function (scope, element, attrs) { 
            element.bind('change', function () { 
                if (element.val() == "") { 
                    element.val(0).triggerHandler('input'); 
                } 
            }) 
        } 
    } 
});

请为空值定义“不工作”。您需要使用ngmodel与输入绑定角度变量,并将默认值设置为0。我想说的是,从1.5开始,您不应该使用$scope,而应该使用控制器。我想说,您应该使用它,因为它适合您的需要,因为角度的版本更改频率高于socks。不,angularjs不会再次更改版本,1.7在LTS中。
myApp.directive('checkEmpty', function () { 
    return { 
        restrict: 'A', 
        link: function (scope, element, attrs) { 
            element.bind('change', function () { 
                if (element.val() == "") { 
                    element.val(0).triggerHandler('input'); 
                } 
            }) 
        } 
    } 
});
$scope.buTotCalc = function(event) {
  event.target.value = event.target.value || 0;
}