pri*_*jef 11 javascript data-binding scope web-applications angularjs
如果您需要更多信息或希望我澄清任何问题,请告诉我.我已经尝试了很多不同的东西来解决这个问题,但还没有找到解决方案.
我是angularJS的新手,我正在尝试构建一个包含多层数据的应用程序.我有一些基本的用户信息存储在控制器PageController的正文范围内.然后我有一个设置表单,使用$ routeParams(带控制器SettingsController)加载,其中包含一些用于模板目的的自定义指令.由于指令是嵌套的,我使用transclusion来加载第一个指令.这一切似乎都很正常.
我的问题是我试图user.firstname从最里面的指令中引用该字段,并希望使用双向数据绑定来允许对文本框所做的更改导致PageController范围内的值也发生变化.我知道很多这类问题是由在ng-model中使用原语引起的,但是我已经尝试将所有内容都放在一个额外的对象中,这样我才能触发原型继承无济于事.我在这做错了什么?
这是我的代码的JSFiddle,尽可能地剥离以隔离问题.在这个例子中,如果我输入外部文本框,它直接在PageController范围内,它将修改内部文本框,直到该文本框被修改,连接断开.这看起来就像其他问题中描述的使用原语的问题,但我无法弄清楚问题出在哪里.
HTML:
<body class="event-listing" ng-app="app" ng-controller="PageController">
<div class="listing-event-wrap">
<input type="text" ng-model="user.firstname" />
<div ng-controller="SettingsController">
<section block title="{{data.updateInfo.title}}" description="{{data.updateInfo.description}}">
<div formrow label="{{data.updateInfo.labels.firstname}}" type="textInput" value="user.firstname"></div>
</section>
</div>
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
角度指令:
app.directive('formrow', function() {
return {
scope: {
label: "@label",
type: "@type",
value: "=value"
},
replace: true,
template: '<div class="form-row">' +
'<div class="form-label" data-ng-show="label">{{label}}</div>' +
'<div class="form-entry" ng-switch on="type">' +
'<input type="text" ng-model="value" data-ng-switch-when="textInput" />' +
'</div>' +
'</div>'
}
});
app.directive('block', function() {
return {
scope: {
title: "@title",
description: "@description"
},
transclude: true,
replace: true,
template: '<div class="page-block">' +
'<h2 data-ng-show="title">{{title}}</h2>' +
'<p class="form-description" data-ng-show="description">{{description}}</p>' +
'<div class="block-inside" data-ng-transclude></div>' +
'</div>'
}
});
Run Code Online (Sandbox Code Playgroud)
角度控制器:
app.controller("PageController", function($scope) {
$scope.user = {
firstname: "John"
};
});
app.controller("SettingsController", function($scope) {
$scope.data = {
updateInfo: {
title: "Update Your Information",
description: "A description here",
labels: {
firstname: "First Name"
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
Nir*_*Nir 11
我很抱歉以前的代码.试试这个:http://jsfiddle.net/CxNc2/2/
我现在正在传递对象+指向内部正确值的指针,而不是传递实际值.我在这里添加了'refobject':
<body class="event-listing" ng-app="app" ng-controller="PageController">
<div class="listing-event-wrap">
<input type="text" ng-model="user.firstname" />
<div ng-controller="SettingsController">
<section block title="{{data.updateInfo.title}}" description="{{data.updateInfo.description}}">
<div formrow label="{{data.updateInfo.labels.firstname}}" type="textInput" refobj='user' value="firstname"></div>
</section>
</div>
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
我在这里添加了refobj +值:
app.directive('formrow', function() {
return {
scope: {
label: "@label",
type: "@type",
value: "@value",
refobj: "="
},
replace: true,
template: '<div class="form-row">' +
'<div class="form-label" data-ng-show="label">{{label}}</div>' +
'<div class="form-entry" ng-switch on="type">' +
'<input type="text" ng-model="refobj[value]" data-ng-switch-when="textInput" />' +
'</div>' +
'</div>'
}
Run Code Online (Sandbox Code Playgroud)
由于指令中的文本框使用原语而不是对象作为其模型(ng-model="value"而不是ng-model="someobj.somevalue"),因此仅在本地范围内创建其模型,而父模型无法访问它.
修复方法是使用点规则作为对象属性来定义指令文本框模型:
ng-model="value.firstname"
Run Code Online (Sandbox Code Playgroud)
然后将整个user对象传递给指令而不仅仅是原始属性:
<div formrow ... value="user"></div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13052 次 |
| 最近记录: |