【问题标题】:AngularJs call parent controller function with parameterAngularJs用参数调用父控制器函数
【发布时间】:2015-04-16 07:42:06
【问题描述】:

我有两个控制器,当用户单击按钮时,我想用参数调用其他父控制器函数。

HTML

<a ng-click='test('something')'>Click</a>

控制器

controller: function($scope) {
        ..........
   $scope.test= $scope.$parent.parentTest(t);// it fails...

..........}

父控制器

$scope.parentTest= function(m) {

   var my=m;

   ...Something...

}

如果我在没有任何参数的情况下运行函数,它就可以工作。如果我使用参数运行函数,则不会。

我想用参数调用父函数。

【问题讨论】:

  • 格式和语法。

标签: javascript angularjs model-view-controller


【解决方案1】:

您的代码中的错误在于这一行:

//This assigns the RESULT of parentTest() to $scope.test
$scope.test= $scope.$parent.parentTest(t);

// 以下两个选项中的任何一个都可能适合您。

//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!

【讨论】:

  • 你能详细说明一下吗?控制台错误?怎么了? $scope.$parent.parentTest 是否未定义?是否调用了 $scope.test?
【解决方案2】:

检查这个 plunkr,也许这可以帮助你

plunkr

access parentfunction in child controller

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-03
    • 2014-12-31
    • 1970-01-01
    • 2014-01-04
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多