javascript AngularJS:在控制器中初始化嵌套范围变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22564977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:22:26  来源:igfitidea点击:

AngularJS: Initialize nested scope variables in controller

javascriptangularjs

提问by Tyler

I like to keep my namespace of model names clean and descriptive, so I use nested model variables like this (where month and year are nested in service):

我喜欢保持我的模型名称命名空间干净和描述性,所以我使用这样的嵌套模型变量(其中月份和年份嵌套在服务中):

<form data-ng-submit="submit()">
  <select data-ng-model="service.month" data-ng-options="m for m in months"/>
  <input data-ng-model="service.year"/> {{ service.year }}
  <input type="submit"/>
</form>

And in my controller, I want to set $scope.service.monthand $scope.service.yearto initial values, but I get a javascript error Cannot set property 'year' of undefined, so I'm guessing the DOM hasn't been parsed and all the $scopevariables haven't been created yet. How can I have some code wait to run until the DOM has been parsed by Angular and all the models have been created?

在我的控制器中,我想设置$scope.service.month$scope.service.year初始值,但是我收到一个 javascript 错误Cannot set property 'year' of undefined,所以我猜 DOM 还没有被解析,所有的$scope变量还没有被创建。如何让一些代码等待运行,直到 DOM 被 Angular 解析并且所有模型都已创建?

Here's my controller:

这是我的控制器:

mod.controller('AddServiceCtrl', [
    '$scope', 
    '$rootScope', 
function($scope, $rootScope) {
  var date = new Date();
  $scope.months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  $scope.service.year = 2014;   
  $scope.submit = function(){
  };
}])

采纳答案by mpm

 $scope.service= {month:'Mar',year:2014};

AngularJS is still JavaScript.

AngularJS 仍然是 JavaScript。

The AngularJS team unfortunately chose to name what is actually a presenter (or a view model), a controller. It might not sound that important, but conceptually it is in order to understand what controllersdo.

不幸的是,AngularJS 团队选择将实际的演示者(或视图模型)命名为controller. 这听起来可能不那么重要,但从概念上讲,它是为了理解做controllers什么。

回答by ryan.l

The problem is that the $scope.serviceobject is undefined, so it is failing to define a yearproperty on it inside the controller, not the view. To make this work, you must set serviceobject on the scope like this:

问题是$scope.service对象是undefined,所以它无法year在控制器内部定义属性,而不是视图。要完成这项工作,您必须service像这样在作用域上设置对象:

$scope.service = {};

then you can define default values for the model on the controller orin the view:

然后您可以在控制器视图中为模型定义默认值:

Controller option

控制器选项

$scope.service = {};
$scope.service.year = 2014;

or

或者

$scope.service = {
    year: 2014,
    month: $scope.months[3]   // to default to 'Apr'
};

View Option

查看选项

<form data-ng-submit="submit()" 
      data-ng-init="service.year=2014; service.month=months[3]">
    ...
</form>

either way, you must create the serviceobject on the scope in order to define properties thereon. use object literal notation ({}) to create it.

无论哪种方式,您都必须service在作用域上创建对象才能在其上定义属性。使用对象文字符号 ( {}) 来创建它。