HTML5技术

angular之$broadcast、$emit、$on传值 - webNick

字号+ 作者:H5之家 来源:H5之家 2017-05-14 18:00 我要评论( )

文件层级 index.html angular之$broadcast、$emit、$on传值go home$state go mainui-sref go main$emit $broadcast $onclick me View Code templates - home.html home$state go main View Code templates - main.html main View Code js - route.js var nic

文件层级

 

index.html

angular之$broadcast、$emit、$on传值go home$state go mainui-sref go main$emit $broadcast $onclick me

View Code

templates - home.html

home$state go main

View Code

templates - main.html

main

View Code

js - route.js

var nickApp = angular.module('nickApp', ['ui.router']) .config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) { $urlRouterProvider.otherwise('/home'); $stateProvider .state('home', { url: '/home', templateUrl: 'templates/home.html', controller: 'HomeCtl', //params: {paramsData: null} navButtons:[] //自定义一些参数 在controll通过$state.get('home')得到对象 }) .state('main', { url: '/main', templateUrl: 'templates/main.html', controller: 'MainCtl', params: {paramsData: null} //目标页面定义接收的参数prameData }) }])

View Code

js - app.js

nickApp .controller('bodyCtl', ['$state', '$scope', function ($state, $scope) { $scope.goMain = function () { $state.go('main', {paramsData: 'body go main'});//传参时参数一致prameData--目标页路由配置params:{paramsData: null} }; $scope.goHome = function () { $state.go('home', {paramsData: 'home page'});//目标页路由未配置params则$stateParams 为空 }; $scope.data = 'body data'; console.log($scope.data); console.log($scope.homeData);//父无法用子scope homeData 使用方法传值接收 $on $emit $broadcast }]) .controller('HomeCtl', ['$scope', '$state', '$stateParams', function ($scope, $state, $stateParams) { $scope.goMain = function () { $state.go('main', {param: 'home go main'});//不一致 $stateParams 接收不到则为目标页面定义接收的参数{paramsData: null} }; console.log($stateParams); console.log($scope.data);//子用父scope data $scope.homeData = 'home data'; console.log($scope.homeData); console.log($state.get('home')); console.log($state.get('main')); }]) .controller('MainCtl', ['$scope', '$stateParams', function ($scope, $stateParams) { console.log($stateParams); console.log($scope.data);//子用父scope data }]) //$emit 子向父 controller 传 event、data -- $broadcast 父传子 -- $on 接收 .controller('SelfCtrl', ['$scope', 'PublicDataService', function ($scope, PublicDataService) { $scope.click = function () { $scope.$broadcast('to-child', 'to-child data'); //父传子 $scope.$emit('to-parent', 'to-parent data'); //子传父 }; $scope.publicData = PublicDataService.publicData; console.log($scope.publicData); }]) .controller('ParentCtrl', ['$scope', 'PublicDataService', function ($scope, PublicDataService) { $scope.$on('to-parent', function (event, data) { console.log('ParentCtrl--' + data); //父controller能得到传给父级值 }); $scope.$on('to-child', function (event, data) { console.log('ParentCtrl--' + data); //父controller得不到传给子级值 }); $scope.publicData = PublicDataService.publicData; console.log($scope.publicData); }]) .controller('ChildCtrl', ['$scope', 'PublicDataService', function ($scope, PublicDataService) { $scope.$on('to-child', function (event, data) { console.log('ChildCtrl--' + data); //子controller能得到传给子级值 }); $scope.$on('to-parent', function (event, data) { console.log('ChildCtrl--' + data); //子controller得不到传给父级值 }); $scope.publicData = PublicDataService.publicData; console.log($scope.publicData); }]) /* 平级得不到值 解决办法 *用上面的$state.go() *或者定义一个公共服务 PublicDataService 注入 controller 就可以使用了 *或 在配置路由时自定义一些参数 在controll--HomeCtl通过$state.get('home')得到一个对象,它包含了配置home页的路由参数 */ .controller('BroCtrl', ['$scope', 'PublicDataService', function ($scope, PublicDataService) { $scope.$on('to-parent', function (event, data) { console.log('BroCtrl', data); //平级得不到值 }); $scope.$on('to-child', function (event, data) { console.log('BroCtrl', data); //平级得不到值 }); $scope.publicData = PublicDataService.publicData; console.log($scope.publicData); }])

View Code

js - service - PublicDataService.js

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 【AngularJS中的自定义服务service VS factory VS provider】---它们的区别,你知道

    【AngularJS中的自定义服务service VS factory VS provider】---它们

    2017-05-08 13:04

  • Angular vs React 最全面深入对比 - 葡萄城控件技术团队

    Angular vs React 最全面深入对比 - 葡萄城控件技术团队

    2017-05-04 16:05

  • 随应潮流-基于ABP+Angularjs现代化应用软件开发框架(1)-总体介绍 - 在路在的张

    随应潮流-基于ABP+Angularjs现代化应用软件开发框架(1)-总体介绍 -

    2017-04-22 08:04

  • NodeJS+MongoDB+AngularJS+Bootstrap书店示例 - Market/Relay

    NodeJS+MongoDB+AngularJS+Bootstrap书店示例 - Market/Relay

    2017-04-14 15:00

网友点评
c