2016-09-22 106 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

相關問題