gpt4 book ai didi

javascript - Angular 为什么不更新值

转载 作者:行者123 更新时间:2023-11-30 12:31:04 24 4
gpt4 key购买 nike

我正在考虑使用 angular,发现了我的第一个问题。可能是初学者的问题。

这是我的 Controller :

(function () {


angular
.module('account.controllers')
.controller('CreateController', CreateController);

CreateController.$inject = ['$location', '$scope', 'Account'];

/**
* @namespace CreateController
*/
function CreateController($location, $scope, Account) {
vm = this;
vm.model = Account.get(1).then(getSuccessFn);


/**
* @name postsSuccessFn
* @desc populate with the data of the current account
*/
function getSuccessFn(data, status, headers, config) {
vm.account = data.data;
}
}
})();

这是我的表格:

<div class="row">
<div class="col-md-4 col-md-offset-4">
<h1>Register</h1>

<div class="well">
<form role="form" name="accountForm" ng-submit="vm.create()" ng-controller="CreateController">
<div class="form-group">
<label for="create__name">name</label>
<input type="text" class="form-control" id="create__name" ng-model="vm.account.name" placeholder="John" />
</div>

<div class="form-group">
<label for="create__payment">Operation Cost</label>
<input type="text" class="form-control" id="create__payment" ng-model="vm.account.payment" placeholder="15.00" />
</div>

<div class="form-group">
<label for="create__member">member</label>
<input type="text" class="form-control" id="create__member" ng-model="vm.account.member" placeholder="Golden" />
</div>

<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>

因此,页面加载正常,数据被检索,vm.account 变量具有正确的值,但值没有显示,它仍然是默认值。为什么?

最佳答案

您是否在使用 controller-as 语法?

要在 HTML 中引用 vm,您应该使用 controller-as 语法。如果您使用的是 ngRoute,请务必在路由配置对象上添加 controllerAs 属性。如果您使用的是 ngController 指令,请执行以下操作:ng-controller="CreateController as vm"

同样在我的测试中,只有当我在 vm 变量声明之前添加 var 关键字时它才有效,如下所示:


var vm = 这个;

这是我用来测试它的代码

<!DOCTYPE html>
<html ng-app="account.controllers">
<head>
<meta charset="utf-8">
<title>stack overflow</title>
<script src="http://code.jquery.com/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<script>
(function () {
angular
.module('account.controllers', [])
.controller('CreateController', CreateController)
.service('Account', ['$q', function($q) {
var srvc = this;
// emulate $http response
srvc.get = function () {
return $q.when({ data : {
name: 'Test',
member: 'Silver',
payment: 10
}
});
};
}]);
CreateController.$inject = ['$location', '$scope', 'Account'];
/**
* @namespace CreateController
*/
function CreateController($location, $scope, Account) {
var vm = this;
vm.model = Account.get(1).then(getSuccessFn);
/**
* @name postsSuccessFn
* @desc populate with the data of the current account
*/
function getSuccessFn(data, status, headers, config) {
vm.account = data.data;
}
}
})();
</script>
</head>
<body>
<div class="row" ng-controller="CreateController as vm">
<div class="col-md-4 col-md-offset-4">
<h1>Register</h1>
<div class="well">
<form role="form" name="accountForm" ng-submit="vm.create()" ng-controller="CreateController">
<div class="form-group">
<label for="create__name">name</label>
<input type="text" class="form-control" id="create__name" ng-model="vm.account.name" placeholder="John" />
</div>

<div class="form-group">
<label for="create__payment">Operation Cost</label>
<input type="text" class="form-control" id="create__payment" ng-model="vm.account.payment" placeholder="15.00" />
</div>

<div class="form-group">
<label for="create__member">member</label>
<input type="text" class="form-control" id="create__member" ng-model="vm.account.member" placeholder="Golden" />
</div>

<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>

关于javascript - Angular 为什么不更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27681284/

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