我想从控制器外部调用一个函数,并将按钮ID传递给它。这是我目前的代码:
<button class="btn btn-primary manageCompany" id=<%=comp._id%> data-toggle="modal" data-target="#manageCompany_pp" ng-click="clickMe()">Manage</button>
// .. More code
<div class="modal fade" ng-controller="ModalCtrl" id="manageCompany_pp" tabindex="-1" cid="" role="dialog" aria-labelledby="manageCompany_pp">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="manageCompany_content">Manage Company</h4>
</div>
<div class="modal-body">
<div class='manageCompanyAlert'></div>
{{testData}}
<script> </script>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
这是我控制器的内容
app.controller('ModalCtrl', ['$scope', '$http', function($scope, $http) {
$scope.clickMe = function() {
$http.post('/modalRoutes/cdata', {
id: 0 // Button ID should go here
}).success(function(cdata) {
$scope.testData = cdata;
});
};
}]);
编辑: 现在似乎最好的方法就是为整个页面创建一个主控制器并从那里处理它。
答案 0 :(得分:2)
您应该能够直接将id传递给函数:
ng-click="clickMe(<%=comp._id%>)"
和
app.controller('ModalCtrl', ['$scope', '$http', function($scope, $http) {
$scope.clickMe = function(id) {
$http.post('/modalRoutes/cdata', {
id: id
}).success(function(cdata) {
$scope.testData = cdata;
});
};
}]);