- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
Update: It must have been something stupid in another part of the code. It works now, so the bindToController syntax is fine.
我们正在使用 AngularJS 1.4,它引入了一个 new way to use bindToController在指令中。
经过相当多的阅读(也许还没有完全理解),我们这样定义了我们的指令:
.directive('mdAddress', function mdAddress() {
var directive = {
restrict: 'EA',
scope: {},
bindToController: {
address: '='
},
templateUrl: 'modules/address/address.html',
controller: AddressController,
controllerAs: 'dir'
};
像这样从另一个 View 调用它:
<md-address address="vm.address"></md-address>
之前在 View Controller 中定义过:
vm.address = {
street: null,
countryCode: null,
cityCode: null,
postalCode: null
};
像这样引用指令模板中的变量:
<md-input-container>
<label>{{'ADDRESSNUMBER' | translate}}</label>
<input type="number" ng-model="dir.address.streetNumber">
</md-input-container>
我们花了 4 小时试图弄清楚为什么我们的指令不起作用。好吧,它工作正常,但 Controller 和指令之间的双向绑定(bind)不起作用,vm.address.street
被无可救药地设置为 null。
一段时间后,我们只是尝试了老办法:
.directive('mdAddress', function mdAddress() {
var directive = {
restrict: 'EA',
scope: {
address: '='
},
bindToController: true,
templateUrl: 'modules/address/address.html',
controller: AddressController,
controllerAs: 'dir'
};
它神奇地起作用了。知道为什么吗?
最佳答案
更新:
感谢 reference to this blog post ,我需要更新我的答案。从 AngularJS 1.4 开始,您似乎真的可以使用
scope: {},
bindToController: {
variable: '='
}
它将做与旧语法(完全)相同的事情:
scope: {
variable: '='
},
bindToController: true
AngularJS 源代码中解释此行为的有用行:
if (isObject(directive.scope)) {
if (directive.bindToController === true) {
bindings.bindToController = parseIsolateBindings(directive.scope,
directiveName, true);
bindings.isolateScope = {};
} else {
bindings.isolateScope = parseIsolateBindings(directive.scope,
directiveName, false);
}
}
if (isObject(directive.bindToController)) {
bindings.bindToController =
parseIsolateBindings(directive.bindToController, directiveName, true);
}
原答案:
希望我能向您解释为什么您遇到的这种行为是正确的,以及您在哪里误解了范围绑定(bind)的概念。
让我解释一下,您在第一个代码片段中做了什么:
.directive('mdAddress', function mdAddress() {
var directive = {
restrict: 'EA',
scope: {},
bindToController: {
address: '='
},
templateUrl: 'modules/address/address.html',
controller: AddressController,
controllerAs: 'dir'
};
使用 scope:{}
,您为 mdAddress
指令创建了一个独立的范围(没有任何继承)。这意味着:父 Controller 和您的指令之间没有数据传递。
考虑到这一点,关于您的第二个代码片段:
<md-address address="vm.address"></md-address>
vm.address
来自您的父 Controller / View 将作为表达式分配给指令的 address 属性,但是由于您之前定义了一个独立的范围,因此数据不会传递到 AddressController
,因此在 bindToController
值中不可用。
让我们将 scope
对象定义视为“将传入哪些数据”,将 bindToController
视为“哪些数据将在我的 View 的 controllerAs 对象中可用” ".
那么,现在让我们看一下最后一个(和工作代码片段):
.directive('mdAddress', function mdAddress() {
var directive = {
restrict: 'EA',
scope: {
address: '='
},
bindToController: true,
templateUrl: 'modules/address/address.html',
controller: AddressController,
controllerAs: 'dir'
};
您也在那里创建了一个独立的作用域,但这次您添加了 address
属性以作为表达式传入。所以现在您从第二个代码段中的 View 传入的 address
将在 Controller 的范围内可用。现在设置 bindToController: true
会将所有当前作用域的属性绑定(bind)到 Controller (或者更可能是 controllerAs 对象)。现在,它按您预期的方式工作,因为数据将传入范围,数据将传出到 Controller 的模板范围。
这个简短的概述是否有助于您更好地理解 scope
和 bindToController
定义对象的概念?
关于javascript - AngularJS 1.4 指令 : scope, 两种方式绑定(bind)和 bindToController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31861783/
我在玩bindToController指令的选项。我偶然发现了使用子作用域与隔离作用域的行为之间看似奇怪的区别。当我使用隔离作用域时,会为指令创建一个新作用域,但对绑定(bind) Controlle
所以我有一个指令,它在创建时应该有一个值。让我们调用指令 MyDirective。要使用它并向它传递一个值,您可以这样做: 我使用的是 TypeScript,所以我想要没有 $scope 的类,为此
我有 actionButtons 指令: function actionButtons(){ 'use strict'; return { scope: {},
我在获取更新 View 的指令时遇到了一些问题。 在我的 Controller 中,我为 的属性设置了初始值。指示。然后,2 秒后,我正在更新 vm.listObjectSelected测试其异步行
因此,我正在阅读有关迁移到 Angular 2.0 的内容,并试图了解指令/组件可以相互通信的更好方式。 我遵循了这里的一些指南,这些指南建议利用基于命名空间的继承在指令之间共享信息。( this )
我正在尝试在指令内绑定(bind)变量,并使用新的 Angular 1.4 bindToController 语法从指令中修改它。 HTML: {{boundVar}} Javascript
从 Angular v1.4 开始,可以这样做: scope: {}, bindToController: { name: "=" } 代替旧的做法: scope: { name: "
我有一个相当简单的 AngularJS 问题,我似乎找不到答案: 我将如何使用 $scope.$watch()在指令 Controller 中同时使用 controllerAs和 bindToCont
我正在使用 Angular 1.5.7 版本。 我有一个指令,它将 Controller 名称和 View 名称作为字符串来分别调用 Controller 。 我无法将用户名绑定(bind)到前一个
我正在学习如何正确使用自定义指令的 bindToController 功能,并想知道如何从指令 Controller 访问您在 bindToController 对象中声明的属性。 var myApp
Update: It must have been something stupid in another part of the code. It works now, so the bindToC
好吧,这真的很烦我。我有一个隔离范围的指令,使用 controllerAs 语法和 bindToController: function exampleDirectiveFactory() {
我已经使用 bindToController 语法创建了一个尽可能简单的指令,由于我所看到的,这导致了信仰的崩溃: function foobar() { return { r
我正在尝试使用 karma 和 jasmine 来测试指令。我正在使用 Angular 1.4,我在 SO 和互联网上搜索了不同的东西,但我无法让它工作。 var angular = require(
我是一名优秀的程序员,十分优秀!