简体   繁体   English

我可以避免使用$ scope。$ watch返回一个未定义的值吗?

[英]Can I avoid that $scope.$watch returns an undefined value?

When I observe a scope variable in Angular via $scope.$watch , it seems to be undefined only at the first call of the watch function. 当我通过$scope.$watch观察Angular中的范围变量时,它似乎仅在watch函数的第一次调用时undefined

Is it possible to rewrite my code to avoid the unnecessary check for undefined ? 是否可以重写我的代码以避免对undefined进行不必要的检查?

Here is a minimal example: 这是一个最小的例子:

1) jsfiddle 1) jsfiddle

2) HTML: 2)HTML:

<div ng-app="MyApp">
   <div ng-controller="MyCtrl">Enter some text:
      <input type="text" ng-model="text" size="30" />
      <p>You entered: {{text}}</p>
      <p>Length: {{textLength}}</p>
   </div>
</div>

3) Javascript: 3)Javascript:

angular.module('MyApp', []).controller(
  'MyCtrl', function ($scope) {
    $scope.textLength = 0;
    $scope.$watch('text', function (value) {
      if (typeof value !== 'undefined') {
        $scope.textLength = value.length;
      } else {
        $scope.textLength = 'observed value is undefined';
      }
    });
});

If you set a default empty value for your watched property in your view model you won't have the problem with undefined values. 如果在视图模型中为监视属性设置默认空值,则不会出现undefined值的问题。

In your case add this before or after $scope.textLength initialization (check this fiddle ). 在你的情况下,在$scope.textLength初始化之前或之后添加它(检查这个小提琴 )。

$scope.text = '';

Another option to avoid the undefined error is you can wrap your function in a hasOwnProperty if statement. 避免未定义错误的另一个选项是您可以将函数包装在hasOwnProperty if语句中。

 $scope.$watch('text', function (value) { if($scope.hasOwnProperty('text')) { if (typeof value !== 'undefined') { $scope.textLength = value.length; } else { $scope.textLength = 'observed value is undefined'; } }); } }; 

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

相关问题 为什么我不能将元素属性传递给范围。$ watch但我可以传递一个返回它的函数 - Why cant I pass an element attribute to scope.$watch but i can pass a function that returns it $ scope。$ watch获取未定义的新旧值angularjs - $scope.$watch getting undefined new and old value angularjs 角度范围。$ eval返回未定义。 我可以检查范围中是否存在表达式? - Angular scope.$eval returns back undefined. Can I check if expression exist in scope? $ scope。$ watch无法观看某些值 - $scope.$watch can not watch some values $ scope。$ watch(var,function)未定义不是函数 - $scope.$watch(var,function) undefined is not a function $ scope。$ watch没有捕获指令修改的值 - $scope.$watch not catching the value modified by directive 当我将$ scope分配给另一个变量并更改新变量的监视值时,不会触发Angular $ scope。$ watch - Angular $scope.$watch is not triggered when i assign $scope to another variable and change the watched value on new variable $ scope或$ scope。$ watch中的范围? - $scope or scope in $scope.$watch? 在指令中使用$ scope。$ watch来处理未定义变量的替代方法 - Alternatives to using $scope.$watch in directive to handle undefined variable AngularJS $ scope。$ watch回调函数参数在Jasmine测试中未定义 - AngularJS $scope.$watch callback function parameter undefined in Jasmine test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM