当前位置: 首页 > news >正文

网站建设销售合作合同范本/今天的新闻 最新消息

网站建设销售合作合同范本,今天的新闻 最新消息,辽宁建设工程信息网入库,电商网站建设规划开发方案路由的事件 事件这个词在前端出现的频率真是高,根本拦不住,哪都是.$route服务在路由过程中的每个阶段都会触发不同的事件,可以为这些不同的路由事件设置监听器并做出响应. 一共有4个事件用来监听路由的状态变化: $routeStartChange, $routeChangeSuccess, $routeChangeError, $…

 

路由的事件

    事件这个词在前端出现的频率真是高,根本拦不住,哪都是.$route服务在路由过程中的每个阶段都会触发不同的事件,可以为这些不同的路由事件设置监听器并做出响应.

    一共有4个事件用来监听路由的状态变化: $routeStartChange, $routeChangeSuccess, $routeChangeError, $routeUpdate.

    其中最常用的是前两个,这里稍微解释一下.

    (1) $routeStartChange

    看名字就能猜出来它表示的是路由开始变化的事件,在浏览器地址栏发生变化之前AngularJS会先广播一下这个事件.路由会开始加载所有需要的依赖,模板和resolve部分的内容也会注入.

1
2
3
4
5
6
angular.module('myApp', [])
  .run(['$rootScope''$location'function($rootScope, $location){
    $rootScope.$on('$routeChangeStart'function(evt, next, current){
    console.log('route begin change');
  }); 
}]);

 

    解释一下事件的参数,evt是事件对象,可以从中读取到一些route的信息.next是将要导航到的路由,current是当前的URL.

    可以看见在这个时期我们可以做很多有用的事,因为此时仅仅是路由开始变化,对应的内容都还没来得及发生改变.这里我们可进行permission的校验,loading画面的加载,对应路由信息的读取等等.

    (2) $routeChangeSuccess

    在路由的所有依赖都被注入加载后,AngularJS会对外广播路由跳转成功的事件.

1
2
3
4
5
6
angular.module('myApp', [])
  .run(['$rootScope''$location'function($rootScope, $location) {
    $rootScope.$on('$routeChangeSuccess'function(evt, current, previous) {
      console.log('route have already changed');
    }); 
}])

 

    这里也稍微解释下三个参数,evt是AngularJS事件对象,current是当前所处路由,previous是上一个路由.

    剩下两个不太常用的事件,大家去看官方API说明吧,这里不介绍了

----------------------------------------------------------------------

  • $emit只能向parent controller传递event与data( $emit(name, args) )
  • $broadcast只能向child controller传递event与data( $broadcast(name, args) )
  • $on用于接收event与data( $on(name, listener) )
click me

本节课程源码:

1
2
3
4
5
6
7
8
9
10
     
<div ng-controller="ParentCtrl">              <!--父级-->
  <div ng-controller="SelfCtrl">              <!--自己-->
    <a ng-click="click()">click me</a>
    <div ng-controller="ChildCtrl"></div>     <!--子级-->
  </div>
  <div ng-controller="BroCtrl"></div>         <!--平级-->
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var app = angular.module('myApp', []);
app.controller('SelfCtrl', function($scope) {
  $scope.click = function () {
    $scope.$broadcast('to-child', 'child');
    $scope.$emit('to-parent', 'parent');
  }
});
app.controller('ParentCtrl', function($scope) {
  $scope.$on('to-parent', function(event,data) {
    console.log('ParentCtrl', data);       //父级能得到值
  });
  $scope.$on('to-child', function(event,data) {
    console.log('ParentCtrl', data);       //子级得不到值
  });
});
app.controller('ChildCtrl', function($scope){
  $scope.$on('to-child', function(event,data) {
    console.log('ChildCtrl', data);      //子级能得到值
  });
  $scope.$on('to-parent', function(event,data) {
    console.log('ChildCtrl', data);      //父级得不到值
  });
});
app.controller('BroCtrl', function($scope){
  $scope.$on('to-parent', function(event,data) {
    console.log('BroCtrl', data);         //平级得不到值
  });
  $scope.$on('to-child', function(event,data) {
    console.log('BroCtrl', data);         //平级得不到值
  });
});

在$on的方法中的event事件参数,其对象的属性和方法如下

事件属性目的
event.targetScope发出或者传播原始事件的作用域
event.currentScope目前正在处理的事件的作用域
event.name事件名称
event.stopPropagation()一个防止事件进一步传播(冒泡/捕获)的函数(这只适用于使用`$emit`发出的事件)
event.preventDefault()这个方法实际上不会做什么事,但是会设置`defaultPrevented`为true。直到事件监听器的实现者采取行动之前它才会检查`defaultPrevented`的值。
event.defaultPrevented如果调用了`preventDefault`则为true

----------------------------------------------------------

 

父传($scope.$broadcast)子接收($scope.$on)
angular.module('myApp', [])
.controller('ParentCtrl', ['$scope', function($scope) {
$scope.message = "Child updated from parent controller";

$scope.clickFunction = function() {
$scope.$broadcast('update_parent_controller', $scope.message);
};
}
])
.controller('ChildCtrl', ['$scope', function($scope) {
$scope.message = "Some text in child controller";

$scope.$on("update_parent_controller", function(event, message) {
$scope.message = message;
});
}
]);

Here a plunker for a live demo.

Instead, if it need to send data from the SecondController (child) to the FirstController (parent), it should use the $emit method.
Here the javascript code:
子传($scope.$emit)父接收($scope.$on)
angular.module('myApp', [])
.controller('ParentCtrl', ['$scope', function ($scope) {
$scope.message = "Some text in parent";
$scope.$on("update_parent_controller", function(event, message){
$scope.message = message;
});

}])
.controller('ChildCtrl', ['$scope', function ($scope) {
$scope.clickFunction = function() {
$scope.message = "Parent updated";

$scope.$emit('update_parent_controller', $scope.message);
}

}]);

Here a plunker for a live demo.

Finally, here a little trick where two controller have no parent/child relationship.
It should pass data from one controller through $rootScope and the $broadcast method.
Here the javascript code:
兄弟传($rootScope.$broadcast)兄弟接收($rootScope.$on)
angular.module('myApp', [])
.controller('FirstCtrl', ['$scope', '$rootScope', function($scope, $rootScope) {
$scope.message = "Clicked!";

$rootScope.clickFunction = function() {
$rootScope.$broadcast("Update", $scope.message);
};
}])
.controller('SecondCtrl', ['$scope','$rootScope', function($scope,$rootScope) {
$scope.message = "Waiting for a click...";

$rootScope.$on("Update", function(event, message) {
$scope.message = message;
});
}]);

Here a plunker for a live demo.

 

----------------------------------------------------------------------------------

 

 

 

 

发送消息: $scope.$emit(name, data) 或者 $scope.$broadcast(name, data);

接收消息: $scope.on(name,function(event,data){ });

区别: $emit 广播给父controller   $broadcast 广播给子controller

 

broadcast 是从发送者向他的子scope广播一个事件。

这里就是ParentController发送, ParentController 和 ChildController 会接受到, 而MainController是不会收到的

 

$emit 广播给父controller,父controller 是可以收到消息

$on 有两个参数function(event,msg)  第一个参数是事件对象,第二个参数是接收到消息信息

 

复制代码
var app = angular.module('onBroadcastEvent', ['ng']);app.controller('MainController', function($scope) {$scope.$on('To-MainController', function(event,msg) {console.log('MainController received:' + msg);});
});app.controller('ParentController', function($scope) {$scope.click = function (msg) {$scope.$emit('To-MainController',msg + ',from ParentController to MainController');$scope.$broadcast('To-ChildController', msg + ',from ParentController to ChildController');$scope.$broadcast('To-BrotherController', msg + ',from ParentController to BrotherController');}
});app.controller('ChildController', function($scope){$scope.$on('To-ChildController', function(event,msg) {console.log('ChildController received:' + msg);});
});app.controller('BrotherController', function($scope){$scope.$on('To-BrotherController', function(event, msg) {console.log('BrotherController received:' + msg);});
});
复制代码

转载于:https://www.cnblogs.com/fx2008/p/5435575.html

http://www.lbrq.cn/news/1330543.html

相关文章:

  • 用vs做网站 怎么安装/网站服务器查询工具
  • 做网站用宋体有版权问题吗/百度指数搜索榜
  • 网站开发一定要用框架吗/推荐友情链接
  • 婚纱摄影东莞网站建设技术支持/全球十大网站排名
  • 钱宝网站怎么做任务/品牌营销公司
  • ppt设计网站有哪些/南宁网站建设公司
  • 商城网站建设定制/我要发布信息
  • 做网站的网页/网络宣传方式
  • wap文字网页游戏/网站建设优化
  • 家政 东莞网站建设/新的营销模式有哪些
  • 戴尔cs24TY可以做网站吗/青岛网站运营
  • 糗百网站开发/快速建站教程
  • 一起做单网站怎么样/郑州建网站的公司
  • 新风向网站建设/快速seo排名优化
  • 做poster网站/23岁老牌网站
  • 深圳做棋牌网站建设有哪些公司/免费引流在线推广
  • 哪里有做枪网站的/百度竞价推广是什么意思
  • 酒厂网站模板/疫情防控最新政策
  • 做防腐木花架的网站/怎么优化网络
  • 宜春网站制作/制作网站公司
  • 运用photoshop设计网站首页/株洲网站设计外包首选
  • 网页怎么制作的/seo网页的基础知识
  • 建设一个asp网站/网站如何优化一个关键词
  • 邯郸软件定制/网站内容优化怎么去优化呢
  • 网站开发基本语言/加盟培训机构
  • 上海做网站哪里有/外链工具软件
  • 汕头网站建设 网络服务/微商怎么引流被别人加
  • 网站怎样做排名靠前/网站推广投放
  • 用卫生纸做的礼物街网站/上海seo网站优化软件
  • 上海专业做网站公司/站外seo推广
  • 我从 Web2 转型到 Web3 的 9 条经验总结
  • 安宝特案例丨AR+AI赋能轨道交通制造:破解人工装配难题的创新实践
  • 8.c语言指针
  • 四、搭建springCloudAlibaba2021.1版本分布式微服务-加入openFeign远程调用和sentinel流量控制
  • python优秀案例:基于python flask实现的小说文本数据分析与挖掘系统,包括K-means聚类算法和LDA主题分析
  • Java面试宝典:MySQL执行原理二