[英]Angular: custom directive not updating controller
I started to learn Angular not so long time ago and I'm trying to understand scope, binding and etc. 不久前我开始学习Angular,并且试图了解范围,绑定等。
I have an order details controller: 我有一个订单明细控制器:
orderApp.controller('OrderDetailsController', ['$http','$routeParams','$scope','config', function($http, $routeParams, $scope, config){
var orderCtrl = this;
orderCtrl.orderId = $routeParams.orderId;
orderCtrl.order = {};
orderCtrl.editingView = false;
...
}]);
On order details page I want to output all information about selected order. 在订单详细信息页面上,我要输出有关所选订单的所有信息。 Also we need to give user ability to edit order.
另外,我们需要赋予用户编辑订单的能力。 Information about editing mode is stored in orderCtrl.editingView.
有关编辑模式的信息存储在orderCtrl.editingView中。
I decided to create custom directive. 我决定创建自定义指令。 If edit mode is off - display text, otherwise display input.
如果关闭了编辑模式-显示文本,否则显示输入。
orderApp.directive('editableText', function(){
return {
restrict: 'E',
scope: {
property: '=property',
editMode: '=editMode'
},
controller: 'OrderDetailsController',
controllerAs: 'orderCtrl',
templateUrl: '/pages/editable-text.html'
}
});
This is template: 这是模板:
<div class="col-xs-8" ng-if="!editMode">{{property}}</div>
<div class="col-xs-8" ng-if="editMode"><input type="text" class="form-control" ng-model="property"></div>
And this is how I use directive in html files: 这就是我在html文件中使用指令的方式:
<editable-text property="orderCtrl.order.coid" edit-mode="orderCtrl.editingView"></editable-text>
Text and input are switching when edit mode is on/off. 当编辑模式打开/关闭时,文本和输入正在切换。 Problem is that orderCtrl.order.coid property is not updated when I change it in input.
问题是,当我在输入中更改orderCtrl.order.coid属性时,该属性未更新。
Before edit property looks like: 编辑属性之前如下所示:
Turn on edit mode and change value: 打开编辑模式并更改值:
Turn off edit mode and we see old value: 关闭编辑模式,我们看到旧值:
Do I need to synchronise controller values and directive scope? 我需要同步控制器值和指令范围吗? I thought that with 2-ways binding it should happen automatically.
我认为2向绑定应该自动发生。 Probably there is any other way to write this functionality?
可能还有其他方法可以编写此功能吗? Will appreciate any help.
将不胜感激。
UPD UPD
Directive code: 指令代码:
orderApp.directive('editableText', function(){
return {
restrict: 'E',
bindToController: {
property: '=property',
editMode: '=editMode'
},
controller: 'OrderDetailsController',
controllerAs: 'orderCtrl',
templateUrl: '/pages/editable-text.html'
}
});
Directive template: 指令模板:
<div class="col-xs-8" ng-if="!orderCtrl.editMode">{{orderCtrl.property}}</div>
<div class="col-xs-8" ng-if="orderCtrl.editMode"><input type="text" class="form-control" ng-model="orderCtrl.property"/></div>
Directive usage: 指令用法:
<editable-text property="orderCtrl.order.coid" edit-mode="orderCtrl.editingView"></editable-text>
I'm not sure that we really need to pass edit-mode attribute. 我不确定我们是否真的需要传递edit-mode属性。
You should use bindToController: { ..scope properties.. }
option here inside your directive to make sure that isolated scope
properties should get bounded to controller this
context. 您应该在指令内的此处使用
bindToController: { ..scope properties.. }
选项,以确保隔离的scope
属性应绑定this
上下文的控制器。
Directive 指示
orderApp.directive('editableText', function(){
return {
restrict: 'E',
bindToController: {
property: '=property',
editMode: '=editMode'
},
controller: 'OrderDetailsController',
controllerAs: 'orderCtrl',
templateUrl: '/pages/editable-text.html'
}
});
Template 模板
<div class="col-xs-8" ng-if="!orderCtrl.editMode">
{{orderCtrl.property}}
</div>
<div class="col-xs-8" ng-if="orderCtrl.editMode">
<input type="text" class="form-control" ng-model="orderCtrl.property"/>
</div>
Note:- this above
bindToController: { ..scope properties.. }
option available for angular 1.4+ versions.注意:-上面的
bindToController: { ..scope properties.. }
选项可用于角度1.4及更高版本。
For Angular 1.3 > version & 1.4 > version you should use former way of doing it by having bindingToController: true
to bind scope variable to controller context & do keep the varaibles inside scope: { ...props... }
对于Angular 1.3> version&1.4> version,您应该使用以前的方法,使
bindingToController: true
将范围变量绑定到控制器上下文,并将变量保留在scope: { ...props... }
scope: {
property: '=property',
editMode: '=editMode'
},
bindToController: true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.