为什么指令不调用我的控制器方法?

时间:2016-09-22 05:45:43

标签: javascript angularjs angularjs-directive angularjs-scope

指令不是调用我的控制器方法。以下是我的代码:

控制器:

void txtPartyState_GotFocus(object sender, EventArgs e)
{
    try
    {
        this.dgvState.Visible = true;
        this.txtPartyCity.Visible = false;
        this.dgvState.Location = new Point(140, 110);
        this.dgvState.Focus();
        this.txtPartyState.Text = "";
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private void txtPartyState_TextChanged(object sender, EventArgs e)
{
    try
    {
        bs.Filter = string.Format("State LIKE '{0}%'", txtPartyState.Text);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

页:

    exports.controller = ['$scope', function($scope) {
        $scope.addParamter = function () {
            console.log("here");
        };

       $scope.editParamter = function (item) {
            console.log(item);
       };
   }];

指令:

JS:

<formula-editor
    add-paramter="addParameter()"
    edit-paramter="editParameter(item)">
</formula-editor>

式-editor.html:

exports.inject = function(app) {
    app.directive('formulaEditor', exports.directive);
    return exports.directive;
};

exports.directive = function () {
    return {
        restrict: 'E',
        templateUrl: '/dist/views/formula-editor.html',
        scope: {
            addParameter: '&',
            editParameter: '&'
        }
    };
};

2 个答案:

答案 0 :(得分:1)

哦,欢迎来到Angular!你在这里发了一个boo boo你的$scope.addParamter()函数名错误(拼写错误),所以你的指令无法在指令标签中找到提到的addParameter()函数。所以只需改变你的主html就好了下面

<formula-editor
    add-paramter="addParameter()"
    edit-paramter="editParameter(item)">
</formula-editor>

<formula-editor
    add-paramter="addParamter()"
    edit-paramter="editParamter(item)">
</formula-editor>

这是你指令的工作demo

答案 1 :(得分:0)

您可以尝试以下代码。它可能适用于您。

exports.directive = function () {
    return {
        restrict: 'E',
        templateUrl: '/dist/views/formula-editor.html',
        scope: {
            addParameter: '&addParamter',
            editParameter: '&editParamter'
        }
    };
};

而不是

<formula-editor
    add-paramter="addParameter()"
    edit-paramter="editParameter(item)">
</formula-editor>

你可以使用

<formula-editor
    addParamter="addParameter()"
    editParamter="editParameter(item)">
</formula-editor>

这会更好。