angularJS:如何在父作用域中调用子作用域函数
发布时间:2020-12-17 09:09:51 所属栏目:安全 来源:网络整理
导读:如何从父范围调用在子范围中定义的方法? function ParentCntl() { // I want to call the $scope.get here}function ChildCntl($scope) { $scope.get = function() { return "LOL"; }} http://jsfiddle.net/wUPdW/ 你可以使用 $broadcast 从父到子: functi
如何从父范围调用在子范围中定义的方法?
function ParentCntl() { // I want to call the $scope.get here } function ChildCntl($scope) { $scope.get = function() { return "LOL"; } } http://jsfiddle.net/wUPdW/
你可以使用
$broadcast 从父到子:
function ParentCntl($scope) { $scope.msg = ""; $scope.get = function(){ $scope.$broadcast ('someEvent'); return $scope.msg; } } function ChildCntl($scope) { $scope.$on('someEvent',function(e) { $scope.$parent.msg = $scope.get(); }); $scope.get = function(){ return "LOL"; } } 工作小提琴:http://jsfiddle.net/wUPdW/2/ 更新:有另一个版本,较少耦合和更多的可测试: function ParentCntl($scope) { $scope.msg = ""; $scope.get = function(){ $scope.$broadcast ('someEvent'); return $scope.msg; } $scope.$on('pingBack',function(e,data) { $scope.msg = data; }); } function ChildCntl($scope) { $scope.$on('someEvent',function(e) { $scope.$emit("pingBack",$scope.get()); }); $scope.get = function(){ return "LOL"; } } 小提琴:http://jsfiddle.net/uypo360u/ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- Bootstrap 第16章 弹出框和警告框插件
- 《数据结构》实验三:栈和队列实验
- webService——学习(5):使用 CXF 开发 REST 服
- 在AngularJS中测试$resource服务
- 006_设置执行命令提示和unset shell function
- 强大!基于拖放布局的 Twitter Bootstrap 网站生
- WebService(一)-->Schema 几个重要的规则
- angularjs – 使用md-button时的ng-transclude错
- angularjs – Angular UI-Router模式删除父状态
- windows下安装php5.2.*,php5.3.*,php5.4.*版本的
热点阅读