javascript - Angular 为什么不更新值

标签 javascript angularjs

我正在考虑使用 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/

相关文章:

JavaScript npm install Heroku,引用错误未定义

javascript - 将表单范围值输出转换为文本

javascript - ng-repeat 中的独特计数器

javascript - 具有 0.01 个步骤的数字输入为 angularjs 中的某些值提供未定义

javascript - HTTP 服务 REST - 如何拥有多个 PUT 方法

javascript - WebGL 场景的三 Angular 形/顶点的安全数量是多少?

javascript - 将 base64 位图插入 rtf

javascript - 在codemirror中更改TextArea的高度和宽度

jquery - 以 Angular 方式制作 SVG 动画

javascript - 如何使用angular js上下移动项目?