如何将angular指令绑定到kendo ui模型

时间:2017-05-23 19:11:52

标签: angularjs kendo-ui

我已经搜索了很多,并且在网上找不到类似的问题和解决方案,但是我一直在墙上撞了太久......

我想通过角度模型/对象传递给指令,这样当它返回一个新元素时,它就绑定到旧模型/对象...... 目前它正在返回该值 我试图让代码尽可能简单直接......

希望这是有道理的:

指令:

(function () {
    angular.module('myMod').directive('myDir', ['$compile',
        function ($compile) {
            return {
                restrict: 'E',
                replace: true,
                scope: {
                    type: '@',
                    val: '@'
                },
                link: function (scope, element, attrs) {                
                    var newEl;                   
                    switch (scope.type) {
                        case "1":
                            newEl = '<input type="text" class="k-textbox" k-ng-model="{{val}}"  />';
                            break;
                        case "2":
                            newEl = '<input kendo-date-picker k-ng-model="{{val}}" />';
                            break;
                    }
                    element.replaceWith($compile(newEl)(scope));

                }
            };
        }
    ]);
})();

HTML:

<div class="row" ng-repeat="item in vm.model.things">
    <div class="form-group">
        <div class="col-sm-6">
            <label class="control-label">{{item.name}}</label>
        </div>
        <div class="col-sm-6">
            <my-dir type="{{item.type}}" value="{{item.val}}" ></my-dir>
        </div>
    </div>
</div>

当我检查元素时,我可以看到它返回值:“这是一个文本框”,我确信我很接近,但我没有接近值/文本,我想“绑定”指令的模型/对象:

<input type="text" class="k-textbox ng-scope" k-ng-model="This is a textbox text">

如果您了解我想要实现的目标,请随时编辑问题,这对其他人更有意义...... 谢谢!

1 个答案:

答案 0 :(得分:0)

你不应该在那里使用插值指令,它会在value属性中添加评估值,而是直接使用val,以便k-ng-model保留{{1}的引用$ scope范围值引用

val