Angularjs测试范围变量是否未定义或为null

时间:2014-11-13 08:53:16

标签: angularjs

我的代码我的控制器:

App.controller('TestCtrl', function ($scope, $http) {
   console.log("CTRL STARTED");

   if (angular.isDefined($scope.test)) <--------------- ERROR
      {
         alert('UNDEFINITA');
         LoadDefault();
      };

我的HTML

 <select class="form-control" ng-options="people.id as people.name for people in      peoples" ng-model="test" ng-change="LoadAnotherSelect()">
    <option value="">Seleziona azienda...</option>
 </select>

如何知道测试变量是否未定义???? 感谢高级

1 个答案:

答案 0 :(得分:4)

你的代码看起来很好, 你得到了什么错误?

如您所见,此代码使用angular.isDefined($scope.name)

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

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
  if (angular.isDefined($scope.name)) {
    alert($scope.name);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
  </body>

</html>