Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么不更新该值_Javascript_Angularjs - Fatal编程技术网

Javascript 为什么不更新该值

Javascript 为什么不更新该值,javascript,angularjs,Javascript,Angularjs,我在用angular思考,发现了我的第一个问题。也许这是一个乞丐问题 这是我的控制器: (function () { angular .module('account.controllers') .controller('CreateController', CreateController); CreateController.$inject = ['$location', '$scope', 'Account']; /** * @namespace CreateCo

我在用angular思考,发现了我的第一个问题。也许这是一个乞丐问题

这是我的控制器:

(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变量的值正确,但没有显示值,它仍然是默认值。为什么?

您没有正确使用controllerAs语法。通过以下方式进行修复:

ng-controller="CreateController as vm"

否则,
vm
是范围对象的一个属性,您从未设置过。

是否将控制器用作语法

要在
HTML
中引用
vm
,应使用控制器作为语法。如果使用的是
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>

堆栈溢出
(功能(){
有棱角的
.module('account.controller',[])
.controller('CreateController',CreateController)
.service('Account',['$q',function($q){
var srvc=此;
//模拟$http响应
srvc.get=函数(){
返回$q.when({数据:{
名称:'测试',
成员:'银牌',
付款:10
}
});
};
}]);
CreateController.$inject=['$location','$scope','Account'];
/**
*@namespace-CreateController
*/
函数CreateController($location、$scope、Account){
var vm=这个;
vm.model=Account.get(1),然后(getSuccessFn);
/**
*@name postsSuccessFn
*@desc用活期账户的数据填充
*/
函数getSuccessFn(数据、状态、标题、配置){
vm.account=data.data;
}
}
})();
登记
名称
运营成本
成员
提交

感谢您的快速回复:)完成。但是数据仍然没有显示:(什么是
Account.get(1)
?是
vm.Account=data.data;
被填充了吗?是的,我用firebug检查过,它被填充了。get(1)是帐户服务的一种方法。1是关键。这是我创建的,所以它应该可以工作。可能是其他原因阻止了在您的案例中工作。感谢演示。问题是rodrigopedra sugested之类的数据。只有数据,而不是数据。数据工作正常。感谢您的响应。只需输入变量,什么也没有发生:(您使用的是哪个版本的angular?另外,您可以共享一些服务代码吗?我想到的一件事是,
data
参数是否在
data
属性中包含有效负载。尝试更改将响应分配给
vm.account=data;
Wow!它工作了!)问题是数据属性。非常感谢!