zoukankan      html  css  js  c++  java
  • Angular1.0与Vue循环指令的区别

    1、获取ng-repeat和v-for循环生成的节点

    比较:

    • ng-repeat生成的节点不能直接获取
      解决方案:自定义一个指令监听ng-repeat渲染完再执行相应的DOM操作
    • v-for生成的节点可以直接获取

    angular

    <!DOCTYPE html>
    <html ng-app="myModule">
        <head>
            <meta charset="UTF-8">
            <title></title>
        </head>
        <body ng-controller="myController">
        <ul>
          <li ng-repeat="item in arr" repeat-finish="renderFinish()">{{item}}</li>
        </ul>
          <script src="js/angular.js"></script>
          <script>
            var app = angular.module('myModule', ['ng']);
            app.controller('myController', function($scope, $http){
              $scope.name = 'mary';
              $scope.arr = [1, 3, 4, 5];
              console.log(document.querySelectorAll('li').length);  // 0
    
              $scope.renderFinish = function() {
                  console.log('渲染完成之后的操作',document.querySelectorAll('li').length);  // 4
              }
            })
    
            // 自定义一个repeat-finish的指令,监听ng-repeat渲染完成后执行的脚步
            app.directive('repeatFinish', function() {
              return {
                link: function(scope, element, attr) {
                  if (scope.$last == true) {
                    scope.$eval(attr.repeatFinish);
                  }
                }
              }
            })
          </script>
        </body>
    </html>
    

    vue

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
        </style>
      </head>
      <body>
        <ul class="box">
            <li v-for="item in arr">{{item}}</li>
        </ul>
    
        <script src="js/vue.js"></script>
        <script>
          var mes = {
            arr: [1, 3, 4, 5]
          }
          new Vue({
            el: '.box', 
            data: mes
          })
          console.log(document.querySelectorAll('li').length);  // 4
        </script>
      </body>
    </html>
    
  • 相关阅读:
    Python【每日一问】38
    Python【每日一问】37
    Shell~echo -e 颜色输出
    Python【每日一问】36
    Python【每日一问】35
    聊聊、Java 命令 第二篇
    聊聊、RabbitMQ 配置文件
    聊聊、Java 命令 第一篇
    聊聊、CA机构认证CSR生成
    聊聊、Tomcat中文乱码和JVM设置
  • 原文地址:https://www.cnblogs.com/Zting00/p/7497645.html
Copyright © 2011-2022 走看看