访问data-bind ="值:var"从指令

时间:2015-01-27 08:30:59

标签: javascript jquery angularjs kendo-ui kendo-grid

我想模仿 kendo-grid 中自定义编辑器的行为,但我遇到了访问传递的对象的问题 data-bind =" value:currentObjInGrid&#34

我有这个示例html

<div ng-app="TestApp">
    <div ng-controller="TestCtrl">
        <div custom-input="MyInput" data-bind="value:Hey"></div>
    </div>
</div>

和javascript

var app = angular.module('TestApp', []);

app.controller('TestCtrl', ['$scope', function($scope){
    $scope.Hey = {
                    A: 'Jude',
                    B: 'John'
                };
}]);

app.directive("customInput", function ($timeout, $compile) {
    return {
        restrict: "EA",
        scope: {
            customInput: '@',
            bind: '='
        },
        transclude: true,          
        template: '<button>Hi</button>',
        controller: function($scope){
            // i want to access the content of 'Hey' here 
            // as 2 way binding or pass by reference
            // but i can't access it

            console.log($scope);
        }
    }
});

这是从数据绑定访问对象的正确方法吗?

任何帮助都将不胜感激。

这里是fiddle

1 个答案:

答案 0 :(得分:0)

你不应该使用data-bind =“value:嘿”因为这是Kendo UI MVVM的一部分。它不适用于AngularJS。如果要获取bind属性的值,可以这样做:

<div custom-input="MyInput" data-bind="Hey"></div>

然后使用$ scope的Hey属性:

    controller: function($scope){
        // i want to access the content of 'Hey' here 
        // as 2 way binding or pass by reference
        // but i can't access it

        console.log($scope.bind);
    }

这是一个现场演示:http://dojo.telerik.com/@korchev/EGoWu