gpt4 book ai didi

javascript - 我收到错误。 Angular .js :13550 TypeError: Cannot read property '$invalid' of undefined

转载 作者:行者123 更新时间:2023-12-03 06:52:26 25 4
gpt4 key购买 nike

我正在尝试复制此代码:http://blog.mgechev.com/2014/02/08/remote-desktop-vnc-client-with-angularjs-and-yeoman/但出现错误(请参阅下面的错误)。

有人可以帮助我吗?

这是我的js代码:

'use strict';

angular.module('clientApp')
.controller('MainCtrl',
function ($scope, $location, VNCClient) {

$scope.host = {};
$scope.host.proxyUrl = $location.protocol() + '://' + $location.host() + ':' + $location.port();

$scope.login = function () {
var form = $scope['vnc-form'];
if (form.$invalid) {
form.$setDirty();
} else {
VNCClient.connect($scope.host)
.then(function () {
$location.path('/vnc')
}, function () {
$scope.errorMessage = 'Connection timeout. Please, try again.';
});
}
};

});

还有我的 html 代码:

<div class="container">

<div class="row" style="margin-top:20px">
<div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3" ng-controller="MainCtrl">
<form role="form" name="vnc-form" novalidate class="css-form">
<fieldset>
<h2>VNC Login</h2>
<hr class="colorgraph">
<div class="form-error" ng-bind="errorMessage"></div>
<div class="form-group">
<input type="text" name="hostname" id="hostname-input" class="form-control input-lg" placeholder="Hostname" ng-model="host.hostname" required ng-minlength="3">
</div>
<div class="form-group">
<input type="number" min="1" max="65535" name="port" id="port-input" class="form-control input-lg" placeholder="Port" ng-model="host.port" required>
</div>
<div class="form-group">
<input type="password" name="password" id="password-input" class="form-control input-lg" placeholder="Password" ng-model="host.password">
</div>
<div class="form-group">
<a href="" class="btn btn-lg btn-primary btn-block" ng-click="login()">Login</a>
</div>
<hr class="colorgraph">
</fieldset>
</form>
</div>
</div>

</div>

我收到此错误:

angular.js:13550 TypeError: Cannot read property '$invalid' of undefined
at Scope.$scope.login (main.js:12)
at fn (eval at <anonymous> (angular.js:14432), <anonymous>:4:206)
at expensiveCheckFn (angular.js:15485)
at callback (angular.js:25018)
at Scope.$eval (angular.js:17229)
at Scope.$apply (angular.js:17329)
at HTMLAnchorElement.<anonymous> (angular.js:25023)
at HTMLAnchorElement.jQuery.event.dispatch (jquery.js:4737)
at HTMLAnchorElement.elemData.handle (jquery.js:4549)

最佳答案

如果必须在 Angular Controller 中访问,表单名称之间不应有“-”。只需将表单名称重命名为“vncForm”即可。

示例:

<form role="form" name="vncForm" novalidate class="css-form">
</form>

$scope.login = function () {
var form = $scope['vncFrom'];
if (form.$invalid) {
form.$setDirty();
}
...
};

关于javascript - 我收到错误。 Angular .js :13550 TypeError: Cannot read property '$invalid' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37434212/

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