无法从子指令调用控制器方法

时间:2014-09-15 09:57:49

标签: angularjs-directive

我有以下指令。当我加载页面时,我收到一条错误,指出' changeEditMode'未定义。我怎样才能调用' controllerInstance.changeEditMode()'来自' editLink'指示? 这是最好的方法吗?

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

//  edit a cell directive
var cellEdit = function () {
return {
    restrict: 'A',
    scope: { cellValue: '@', editMode: '@', action: '&' },
    link: function (scope, element, attrs) {
        if (scope.editMode === 'true')
            element.html('<input id="' + attrs.id + '" type="text" value="' + scope.cellValue + '"/>');
        else
            element.html(scope.cellValue);
    },
    controller: function ($scope) {


        this.changeEditMode= function () {
            console.log('test1');
        };
    }
};
};
app.directive('cellEdit', cellEdit);


var editLink = function () {

return {
    restrict: 'A',
    require: "?cellEdit",
    link: function (scope, element, attrs, controllerInstance) {
        element.html('<a>Edit</a>');

        element.bind("click", function () {

        controllerInstance.changeEditMode();



        });
    }        
};
};
app.directive('editLink', editLink);


<div ng-app="jayDirectives">
<table>
<tr>
 <td cell-Edit >
 value1
 </td>

<td edit-Link >
 value2
</td>

</tr>
<tr>
<td cell-Edit >
value3
</td>

<td edit-Link >
 value4
</td>

</tr>
</table>
</div>

0 个答案:

没有答案