【发布时间】:2013-01-22 23:41:35
【问题描述】:
我想如果我将 ng-controller="GeneralInfoCtrl" 打在我的 DOM 中的多个元素上,它们将共享相同的 $scope(或者至少双向绑定不起作用)。
我想这样做的原因是因为我在 HTML 的非常不同的部分有不同的只读视图和关联的模式对话框,并且它们不共享一个共同的祖先(除了<body> 和 <html>) .
有没有办法让两个控制器都引用同一个对象/使数据绑定在它们之间起作用?
这里有一些代码供那些坚持看到标记的人使用,用 Jade 编写:
.client-box(ng-controller="GeneralInfoCtrl")
.box-header
.box-title
h5 General Information
.box-buttons
button.btn.btn-small(data-target='#editGeneralInfo', data-toggle='modal', data-backdrop='static') <i class="icon-pencil"></i> Edit
.box-body
table.table.table-tight.table-key-value
tr
th Name
td {{client.fullName()}}
tr
th Also Known As
td {{client.aka}}
tr
th Birth Date
td {{client.birthDate|date:'mediumDate'}}
...
#editGeneralInfo.modal.hide.fade(ng-controller="GeneralInfoCtrl")
.modal-header
button.close(type='button', data-dismiss='modal') ×
h3 Edit General Information
.modal-body
form.form-horizontal.form-condensed
.control-group
label.control-label First Name
.controls
input(type='text', placeholder='First Name', ng-model='client.firstName')
.control-group
label.control-label Last Name
.controls
input(type='text', placeholder='Last Name', ng-model='client.lastName')
.control-group
label.control-label Also Known As
.controls
input(type='text', placeholder='AKA', ng-model='client.aka')
.control-group
label.control-label Birth Date
.controls
input(type='text', placeholder='MM/DD/YYYY', ng-model='client.birthDate')
...
还有我的控制器:
function GeneralInfoCtrl($scope) {
$scope.client = {
firstName: 'Charlie',
lastName: 'Brown',
birthDate: new Date(2009, 12, 15),
...
}
}
【问题讨论】:
标签: angularjs