gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:02:09 26 4
gpt4 key购买 nike

我的代码我的 Controller :

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>

如何知道测试变量是否未定义???
提前致谢

最佳答案

你的代码看起来不错,
你遇到了什么错误?

如您所见,此代码有效,使用 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>

关于Angularjs 测试范围变量是否未定义或为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26904727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com