gpt4 book ai didi

javascript - Angular 未发布到 URI

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

当我在表单中输入数据时,我的 Angular 代码不会发布到 api,即使我的 api 已被证明可以工作并接受curl CRUD 请求。从我的部分调用注册函数,然后注册 Controller 使用用户服务和注册函数将模型发布到 api

用户服务

(function () {
'use strict';

angular
.module('app')
.factory('UserService', UserService);

UserService.$inject = ['$http'];
function UserService($http) {
var service = {};

service.GetAll = GetAll;
service.GetById = GetById;
service.GetByUsername = GetByUsername;
service.Create = Create;
service.Update = Update;
service.Delete = Delete;

return service;

function GetAll() {
return $http.get('https://me.com/api/users/').then(handleSuccess, handleError('Error getting all users'));
}

function GetById(id) {
return $http.get('https://me.com/api/users/' + id).then(handleSuccess, handleError('Error getting user by id'));
}
...

function Create(user) {
return $http.post('https://me.com/api/users', user).then(handleSuccess, handleError('Error creating user'));
}

.....

注册部分

<div ng-controller="RegisterController as vm">
<div class="col-md-6 col-md-offset-3">
<h2>Register</h2>
<form name="form" ng-submit="vm.register()" role="form">
<div class="form-group" ng-class="{ 'has-error': form.fname.$dirty && form.fname.$error.required }">
<label for="username">First name</label>
<input type="text" name="fname" id="fname" class="form-control" ng-model="vm.user.fname" required />
<span ng-show="form.fname.$dirty && form.fname.$error.required" class="help-block">First name is required</span>
</div>
<div class="form-group" ng-class="{ 'has-error': form.lname.$dirty && form.lname.$error.required }">
<label for="username">Last name</label>
<input type="text" name="lname" id="Text1" class="form-control" ng-model="vm.user.lname" required />
<span ng-show="form.lname.$dirty && form.lname.$error.required" class="help-block">Last name is required</span>
</div>
<div class="form-group" ng-class="{ 'has-error': form.username.$dirty && form.username.$error.required }">
<label for="username">Username</label>
<input type="text" name="username" id="username" class="form-control" ng-model="vm.user.username" required />
<span ng-show="form.username.$dirty && form.username.$error.required" class="help-block">Username is required</span>
</div>
<div class="form-group" ng-class="{ 'has-error': form.hashword.$dirty && form.hashword.$error.required }">
<label for="hashword">Password</label>
<input type="hashword" name="hashword" id="hashword" class="form-control" ng-model="vm.user.hashword" required />
<span ng-show="form.hashword.$dirty && form.hashword.$error.required" class="help-block">Password is required</span>
</div>
<div class="form-actions">
<button type="submit" ng-disabled="form.$invalid || vm.dataLoading" class="btn btn-primary">Register</button>
<img ng-if="vm.dataLoading" src="data:image/gif;base64,..." />
<a href="#/login" class="btn btn-link">Cancel</a>
</div>
</form>
</div>

</div>

注册 Controller /函数

(function () {
'use strict';

angular
.module('app')
.controller('RegisterController', RegisterController);

RegisterController.$inject = ['UserService', '$location', '$rootScope', 'FlashService'];
function RegisterController(UserService, $location, $rootScope, FlashService) {
var vm = this;

vm.register = register;

function register() {
vm.dataLoading = true;
UserService.Create(vm.user)
.then(function (response) {
if (response.success) {
FlashService.Success('Registration successful', true);
$location.path('/login');
} else {
FlashService.Error(response.message);
vm.dataLoading = false;
}
});
return false;
}
}

})();

最佳答案

您如何将 Controller 分配给 View ?您使用过controller as吗?句法?例如: <div ng-controller="RegisterController as vm">your view here </div>您正在将寄存器功能分配给 vm但 vm 只是 Controller 函数中的局部变量,因此 Angular 无法在 View 中使用其函数。分配this本地 vm 变量是一个很好的实践,比到处使用 $scope 更好,但它仍然只是 Controller 函数范围内的本地变量。如果您分配 Controller 来查看并重命名 Controller ( <div ng-controller="RegisterController as vm"> ),那么它将起作用。

关于javascript - Angular 未发布到 URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36328297/

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