zoukankan      html  css  js  c++  java
  • 关于angular.extend的用法

    ng中的ng-function中会有些方法,便于我们进行js代码的编写

    关于angular.extend(dst, src);通过从src对象复制所有属性到dst来扩展目标对象dst。你可以指定多个src对象。

    注意:angular.extend(....)只是简单的对象之间的相互引用,

    经典的demo ;

    <!DOCTYPE html>
    <html ng-app="extendApp">
    <head>
    <meta charset="UTF-8">
    <title></title>
    <script src="http://code.angularjs.org/1.2.3/angular.min.js"></script>
    </head>
    <body>

    <div ng-controller="extendController">

    <button ng-click="extend()">点击我!</button>

    </div>
    </body>
    </html>

    <script type="text/javascript">
    angular.module("extendApp", [])
    .controller("extendController", function($scope)
    {
    $scope.baby =
    {
    cry : function()
    {
    console.log("I can only cry!");
    }
    }

    $scope.adult =
    {
    earn : function()
    {
    console.log("I can earn money!");
    },
    lover:
    {
    love:function()
    {
    console.log("I love you!");
    }
    }
    }
    $scope.human = {}

    $scope.hehe = "hehe ";

    $scope.extend = function()
    {
    angular.extend($scope.human, $scope.baby, $scope.adult);
    $scope.human.cry();
    $scope.human.earn();
    // $scope.human 和$scope.adult其实引用的是同一个对象-
    $scope.human.lover.love = function()
    {
    console.log("I hate you!");
    }

    //这两行都会输出“I hate you !",可怜的adult对象, 他把自己的lover分享给了human! -->
    $scope.human.lover.love();
    $scope.adult.lover.love();
    }
    });
    </script>

    结果:

      I can only cry!
      I can earn money!
      I hate you!
      I hate you!

    拓展:关于对象,数组的复制,我们可以进一步的了解angular.copy();的使用方法

  • 相关阅读:
    CF1454F Array Partition
    leetcode1883 准时抵达会议现场的最小跳过休息次数
    leetcode1871 跳跃游戏 VII
    leetcode1872 石子游戏VIII
    CF1355C Count Triangles
    CF1245D Shichikuji and Power Grid
    CF1368C Even Picture
    CF1368D AND, OR and square sum
    CF1395C Boboniu and Bit Operations
    SpringBoot和开发热部署
  • 原文地址:https://www.cnblogs.com/evaling/p/6695024.html
Copyright © 2011-2022 走看看