假设名为cOne的控制器具有函数
function showAlert() {
alert("Hello, people!");
}
我的情况如下所示
<div id='parent' ng-controller='cOne'>
<span ng-click='cOne.showAlert()'>Click For Alert</span>
<div id='child' ng-controller='cTwo'>
<span ng-click='$parent.cOne.showAlert()'>Click For Parent Controller Alert </span>
</div>
</div>
我可以以某种方式从“child”div调用该cOne控制器的功能吗?我试图使用$ scope,$ rootScope和$ parent。我该怎么做?
答案 0 :(得分:1)
您应该将函数绑定到scope
或控制器实例。如果它是绑定范围
$scope.showAlert = function () {
alert("Hello, people!");
}
你可以像
一样打电话给它<span ng-click='$parent.showAlert()'>Click For Parent Controller Alert </span>
如果将其绑定到控制器实例,您就可以通过刚刚发布的方式调用它。
答案 1 :(得分:1)
您只需将showAlert
分配给$scope
cOne
控制器的$parent
,并从孩子那里拨打angular.module('myApp', [])
.controller('cOne', function($scope) {
$scope.showAlert = function() {
alert('cOne alert');
};
})
.controller('cTwo', function($scope) {});
即可。
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="cOne">
<span ng-click='showAlert()'>Click For Alert-1</span>
<div ng-controller="cTwo">
<span ng-click='$parent.showAlert()'>Click For Alert-2</span>
</div>
</div>
SELECT T.instnum, T.batchnum, T.recdate,
T.operator, T.booknum, T.bpagenum,
T.epagenum, T.nonstandarddoc,
E.returntoname, S.receivedfrom
FROM TBL_Transactions T
inner join TBL_TransEfile S on S.instnum = T.instnum
inner join EFL_Trans E on E.batchnum = S.mosbatchnum
where T.nonstandarddoc = 'y'
and T.`year` = '2017'
and T.efiledocument = 'y'
order by instnum asc