admin管理员组文章数量:1325229
I have two controller's and I want to call other parent controller function with parameter when user clicks the button.
Html
<a ng-click='test('something')'>Click</a>
Controller
controller: function($scope) {
..........
$scope.test= $scope.$parent.parentTest(t);// it fails...
..........}
Parent Controller
$scope.parentTest= function(m) {
var my=m;
...Something...
}
If I run function without any parameter, it works. If I run the function with parameter it doesn't.
I want to call parent function with parameter.
I have two controller's and I want to call other parent controller function with parameter when user clicks the button.
Html
<a ng-click='test('something')'>Click</a>
Controller
controller: function($scope) {
..........
$scope.test= $scope.$parent.parentTest(t);// it fails...
..........}
Parent Controller
$scope.parentTest= function(m) {
var my=m;
...Something...
}
If I run function without any parameter, it works. If I run the function with parameter it doesn't.
I want to call parent function with parameter.
Share Improve this question edited Apr 20, 2015 at 6:22 Suhaib Janjua 3,57216 gold badges69 silver badges87 bronze badges asked Apr 16, 2015 at 7:42 user4773604user4773604 4513 gold badges9 silver badges16 bronze badges 1- Formatting and grammar. – Linda Lawton - DaImTo Commented Apr 16, 2015 at 7:56
2 Answers
Reset to default 4The mistake in your code is with this line:
//This assigns the RESULT of parentTest() to $scope.test
$scope.test= $scope.$parent.parentTest(t);
// Either of the 2 options below will probably work for you.
//This assigns the parentTest Function itself to $scope.test
$scope.test= $scope.$parent.parentTest;
//This wraps it safely in case the parentTest function isn't ready when
// this controller initializes
$scope.test= function(t){$scope.$parent.parentTest(t)}; // << Change it to this!
Check this plunkr, maybe this can help you
plunkr
access parentfunction in child controller
本文标签: javascriptAngularJs call parent controller function with parameterStack Overflow
版权声明:本文标题:javascript - AngularJs call parent controller function with parameter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742170997a2426711.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论