[英]How to update k-ng-model (a.k.a. two-way binding like with ng-model)
Does Kendo k-ng-model
support two-way binding, and if not, what is the best approach to simulate it? Kendo
k-ng-model
是否支持双向绑定,如果不支持,那么模拟它的最佳方法是什么?
A bit of context: 一点背景:
In angular, when an update to ng-model is done, all interested parties are updated (eg view is refreshed). 在角度中,当完成对ng模型的更新时,更新所有感兴趣的各方(例如,刷新视图)。 In kendo, when using k-ng-model, I cannot find a way of doing the same, where I would want to set the value from the controller / directive directly.
在kendo中,当使用k-ng-model时,我找不到相同的方法,我想直接设置controller / directive的值。
Here is an example: Coding Dojo 这是一个例子: 编码Dojo
And, just in case, the code as well: 而且,以防万一,代码也是如此:
<input kendo-auto-complete
k-ng-model="vm.kendoSelection"
ng-model="vm.angularSelection"
k-data-source="vm.countryNames"
k-data-value-field="'name'"
k-data-text-field="'name'" />
sample controller: 样品控制器:
angular.module("MyApp", [ "kendo.directives" ])
.controller("Controller", function(){
// just some data source
this.countryNames = [
{ name: "Albania" },
{ name: "Andorra" }
];
// how to set the k-ng-model here, so that it is also propagated properly (like on
// the view, and that other events like k-on-change work)?
this.kendoSelection = { name: "Albania" };
});
EDIT: 编辑:
Even after the answer from Dion Dirza, the k-on-change is not firing (altough it is a solution in good direction) 即使在Dion Dirza的回答之后,k-on-change也没有解雇(尽管这是一个方向很好的解决方案)
Example with k-on-change k-on-change的例子
Dion Dirza Dion Dirza
As documentation said, that k-ng-model
should store or return actual type of widget value. 正如文档所说,
k-ng-model
应该存储或返回实际类型的小部件值。 Therefore to make your variable kendoSelection
work, you should define it as actual type of widget value , which in this case is array type for auto-complete widget. 因此,要使变量
kendoSelection
正常工作,您应该将其定义为窗口小部件值的实际类型 ,在本例中为自动完成窗口小部件的数组类型。
Change your code 改变你的代码
vm.kendoSelection = [{ name: "Albania" }];
Because you are storing object array here so your variable should be array of object. 因为你在这里存储对象数组所以你的变量应该是对象的数组。 Put a caution here that your object should contains a property that defined as widget data-value-field which it will be used for comparison with current data source.
请注意,您的对象应包含一个定义为widget data-value-field的属性,该属性将用于与当前数据源进行比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.