2016-09-22 119 views
0

指令不调用我的控制器方法。以下是我的代码:为什么指令不会调用我的控制器方法?

控制器:

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

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

页:

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

指令:

JS:

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: '&' 
     } 
    }; 
}; 

配方editor.html:

<button ng-click="addParameter()"></button> 
+0

向我们展示控制器的用法,你在哪里引用它? –

+0

修正你的拼写错误在html:'add-parameter'和'edit-parameter' – devqon

回答

0

你可以试试下面的code.It可能适用于你。

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> 

这将是更好的。

1

哦,欢迎来到Angular!你犯了一个布布这里你$scope.addParamter()函数名是在HTML错误(拼写错误),那么你的指令不能够找到提到addParameter()功能指令tag.So只是改变你的主HTML像下面

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

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

这是你的指令工作demo

相关问题