什么是AngularJS中的Jquery $(this).val()等价物?如果更新后输入字段为空,如何将输入字段值设置为零?

时间:2021-04-20 20:26:53
<script>
$(document).on('change', '.botentry', function () {
    if ($(this).val() == "") {
        $(this).val(0);
    }
});
</script>

and my html code is

我的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>

I added this code to avoid entering empty field in input box, but it's not working with AngularJs, Jquery value zero is not binding with angularjs ng-model.

我添加了这段代码以避免在输入框中输入空字段,但是它不能与AngularJs一起使用,Jquery值零不与angularjs ng-model绑定。

3 个解决方案

#1


1  

Create custom directive (check-Empty) as given below and add this directive to your HTML code like this

创建自定义指令(check-Empty),如下所示,并将此指令添加到您的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"  check-Empty /></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" check-Empty /></div></td>
 <td><div><span ng-if="!b.freeze.m3 || !b.m3.isEdit">{{b.m.value}}</span><input class="botentry" ng-if="b.freeze.m3 && b.m3.isEdit" ng-model="b.m3.value"  ng-blur="buTotCalc()" type="number" check-Empty/></div></td>
</tr>

Add this directive to your code

将此指令添加到您的代码中

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

#2


3  

in angular js, you use $scope:

在角度js中,您使用$ scope:

You don't update the input, you update the model to which the input is bound.

您不更新输入,更新输入绑定的模型。

for example:

your html:

<div ng-controller="MyCtrl">
    <input type='text' ng-model='input' ng-click="myFunction('some value')">
</div>

your controller:

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

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

#3


0  

You can just pass $event as parameter to your buTotCalc function and use $event.target.value to get the value of the element in question.

您可以将$ event作为参数传递给buTotCalc函数,并使用$ event.target.value来获取相关元素的值。

$scope.buTotCalc = function(event) {
  event.target.value = event.target.value || 0;
}

#1


1  

Create custom directive (check-Empty) as given below and add this directive to your HTML code like this

创建自定义指令(check-Empty),如下所示,并将此指令添加到您的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"  check-Empty /></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" check-Empty /></div></td>
 <td><div><span ng-if="!b.freeze.m3 || !b.m3.isEdit">{{b.m.value}}</span><input class="botentry" ng-if="b.freeze.m3 && b.m3.isEdit" ng-model="b.m3.value"  ng-blur="buTotCalc()" type="number" check-Empty/></div></td>
</tr>

Add this directive to your code

将此指令添加到您的代码中

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

#2


3  

in angular js, you use $scope:

在角度js中,您使用$ scope:

You don't update the input, you update the model to which the input is bound.

您不更新输入,更新输入绑定的模型。

for example:

your html:

<div ng-controller="MyCtrl">
    <input type='text' ng-model='input' ng-click="myFunction('some value')">
</div>

your controller:

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

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

#3


0  

You can just pass $event as parameter to your buTotCalc function and use $event.target.value to get the value of the element in question.

您可以将$ event作为参数传递给buTotCalc函数,并使用$ event.target.value来获取相关元素的值。

$scope.buTotCalc = function(event) {
  event.target.value = event.target.value || 0;
}