首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >angularJS:如何在父范围内调用子范围函数

angularJS:如何在父范围内调用子范围函数
EN

Stack Overflow用户
提问于 2013-09-27 05:11:15
回答 2查看 88.6K关注 0票数 96

如何从父范围调用子范围中定义的方法?

function ParentCntl() {
    // I want to call the $scope.get here
}

function ChildCntl($scope) {
    $scope.get = function() {
        return "LOL";    
    }
}

http://jsfiddle.net/wUPdW/

EN

回答 2

Stack Overflow用户

发布于 2014-12-14 22:02:31

让我提出另一种解决方案:

var app = angular.module("myNoteApp", []);


app.controller("ParentCntl", function($scope) {
    $scope.obj = {};
});

app.controller("ChildCntl", function($scope) {
    $scope.obj.get = function() {
            return "LOL";    
    };
});

更少的代码,并使用原型继承。

Plunk

票数 35
EN

Stack Overflow用户

发布于 2018-08-14 21:20:43

你可以创建子对象。

var app = angular.module("myApp", []);


app.controller("ParentCntl", function($scope) {
    $scope.child= {};
    $scope.get = function(){
      return $scope.child.get(); // you can call it. it will return 'LOL'
    }
   // or  you can call it directly like $scope.child.get() once it loaded.
});

app.controller("ChildCntl", function($scope) {
    $scope.obj.get = function() {
            return "LOL";    
    };
});

这里的子节点是get方法的目标。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19038778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档