指令与控制器交互,指令与指令交互

我会带着你远行 2022-08-20 12:24 345阅读 0赞

慕课网《AngularJS实战》–指令3笔记,点击打开链接

一、不同指令对应不同控制器方法(指令的复用)

Directive&Controller.js

  1. var myModule = angular.module("MyModule", []);
  2. myModule.controller('MyCtrl', ['$scope', function($scope){
  3. $scope.loadData=function(){
  4. console.log("加载数据中...");
  5. }
  6. }]);
  7. myModule.controller('MyCtrl2', ['$scope', function($scope){
  8. $scope.loadData2=function(){
  9. console.log("加载数据中...22222");
  10. }
  11. }]);
  12. myModule.directive("loader", function() {
  13. return {
  14. restrict:"AE",
  15. link:function(scope,element,attrs){
  16. element.bind('mouseenter', function(event) {
  17. //scope.loadData();
  18. // scope.$apply("loadData()");
  19. // 注意这里的坑,howToLoad会被转换成小写的howtoload
  20. scope.$apply(attrs.howtoload);
  21. });
  22. }
  23. }
  24. });

Directive&Controller.html

  1. <!doctype html>
  2. <html ng-app="MyModule">
  3. <head>
  4. <meta charset="utf-8">
  5. </head>
  6. <body>
  7. <div ng-controller="MyCtrl">
  8. <loader howToLoad="loadData()">滑动加载</loader>
  9. </div>
  10. <div ng-controller="MyCtrl2">
  11. <loader howToLoad="loadData2()">滑动加载</loader>
  12. </div>
  13. </body>
  14. <script src="framework/angular-1.3.0.14/angular.js"></script>
  15. <script src="Directive&Controller.js"></script>
  16. </html>

二、指令间的交互
以下示例:四种能力越来越强的超人
Directive&Directive.js

  1. var myModule = angular.module("MyModule", []);
  2. myModule.directive("superman", function() {
  3. return {
  4. scope: {},
  5. restrict: 'AE',
  6. controller: function($scope) {
  7. $scope.abilities = [];
  8. this.addStrength = function() {
  9. $scope.abilities.push("strength");
  10. };
  11. this.addSpeed = function() {
  12. $scope.abilities.push("speed");
  13. };
  14. this.addLight = function() {
  15. $scope.abilities.push("light");
  16. };
  17. this.addFart = function() {
  18. $scope.abilities.push("fart");
  19. };
  20. },
  21. link: function(scope, element, attrs) {
  22. element.addClass('btn btn-primary');
  23. element.bind("mouseenter", function() {
  24. console.log(scope.abilities);
  25. });
  26. }
  27. }
  28. });
  29. myModule.directive("strength", function() {
  30. return {
  31. require: '^superman',
  32. link: function(scope, element, attrs, supermanCtrl) {
  33. supermanCtrl.addStrength();
  34. }
  35. }
  36. });
  37. myModule.directive("speed", function() {
  38. return {
  39. require: '^superman',
  40. link: function(scope, element, attrs, supermanCtrl) {
  41. supermanCtrl.addSpeed();
  42. }
  43. }
  44. });
  45. myModule.directive("light", function() {
  46. return {
  47. require: '^superman',
  48. link: function(scope, element, attrs, supermanCtrl) {
  49. supermanCtrl.addLight();
  50. }
  51. }
  52. });
  53. myModule.directive('fart', function(){
  54. return {
  55. require: '^superman',
  56. link: function(scope, element, attrs, supermanCtrl) {
  57. supermanCtrl.addFart();
  58. }
  59. };
  60. });

Directive&Directive.html
controller:中写你想要暴露给外部的方法
link:绑定事件、数据
D

  1. <!doctype html>
  2. <html ng-app="MyModule">
  3. <head>
  4. <meta charset="utf-8">
  5. <link rel="stylesheet" href="css/bootstrap-3.0.0/css/bootstrap.css">
  6. <script src="framework/angular-1.3.0.14/angular.js"></script>
  7. <script src="Directive&Directive.js"></script>
  8. </head>
  9. <body>
  10. <div class="row">
  11. <div class="col-md-3">
  12. <superman strength>动感超人---力量</superman>
  13. </div>
  14. </div>
  15. <div class="row">
  16. <div class="col-md-3">
  17. <superman strength speed>动感超人2---力量+敏捷</superman>
  18. </div>
  19. </div>
  20. <div class="row">
  21. <div class="col-md-3">
  22. <superman strength speed light>动感超人3---力量+敏捷+发光</superman>
  23. </div>
  24. </div>
  25. <div class="row">
  26. <div class="col-md-4">
  27. <superman strength speed light fart>动感超人3---力量+敏捷+发光+放屁</superman>
  28. </div>
  29. </div>
  30. </body>
  31. </html>

发表评论

表情:
评论列表 (有 0 条评论,345人围观)

还没有评论,来说两句吧...

相关阅读

    相关 机器语言指令

    机器语言与指令 每种CPU,都有自己的指令系统。这个指令系统,就是该CPU的机器语言。机器语言是一组有0和1组成的指令码,这些指令码是CPU制造厂商规定出来的,所以不同型号