Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript angular js:函数在单独块中返回的数组值_Javascript_Html_Css_Angularjs - Fatal编程技术网

Javascript angular js:函数在单独块中返回的数组值

Javascript angular js:函数在单独块中返回的数组值,javascript,html,css,angularjs,Javascript,Html,Css,Angularjs,制作一个需要使用angular js数组的程序,我需要以良好的方式显示函数返回的angular js数组元素。就像第一个包含第一个元素,第二个包含第二个元素,依此类推。。。在将来执行某些任务时,必须使用函数。 提前谢谢 例如:- 第一次尝试:- 第一个元素=23 第二个元素=45 第二次尝试:- 第一个元素=20 第二个元素=15 第三次尝试。。。第四次尝试等等 文件。写(“”); 去除 添加 {{x=test()}} 第一次尝试={x.first | number:0}} seco

制作一个需要使用angular js数组的程序,我需要以良好的方式显示函数返回的angular js数组元素。就像第一个包含第一个元素,第二个包含第二个元素,依此类推。。。在将来执行某些任务时,必须使用函数。 提前谢谢 例如:-

第一次尝试:- 第一个元素=23 第二个元素=45

第二次尝试:- 第一个元素=20 第二个元素=15

第三次尝试。。。第四次尝试等等


文件。写(“”);
去除
添加
{{x=test()}}

  • 第一次尝试={x.first | number:0}}
    second second={x.second | number:0}
  • var-app=angular.module(“plunker”,[]); app.controller(“MainCtrl”、[“$scope”、函数($scope){ $scope.test=函数() { var结果=1,id2,a,b; $scope.result1=[{}]; angular.forEach($scope.selects,函数(值) {//在数组上循环以处理所有项 a=值.var1; b=值.var2; 警报(a); $scope.result1.push({name:$scope.selecting.var1}); //myarray={“第一”:a,“第二”:b} //a[‘第一’,‘第二’]=(a,b); 返回结果1; }) } $scope.selects=[{}];//默认设置为1组 //要添加/删除的函数-------------------------------------------------------------------------------- $scope.add=函数() { $scope.selects.push({}); } $scope.remove=功能(项) { angular.forEach($scope.selects,函数(值,键) { 如果(值==项目) { $scope.selects.splice(键1); } }); } //要添加/删除的函数--------------------------------------------------------------------------------- }]);
    我不知道您在
    test()
    函数中想要实现什么,但是如果您只是想显示模型的内容,那么似乎不需要为此创建函数:Angular为您保持模型同步,这是框架的主要卖点之一

    这就是你想要达到的目标吗

    
    文件。写(“”);
    去除
    添加
    做点什么
    文本框组模型
    美元指数
    TxtBox1
    TxtBox2
    {{$index}}
    {{textboxs.textBoxOne.value}
    {{textboxs.textBoxTwo.value}
    字符计数:{{charCount}}
    经过某些处理后的数据
    美元指数
    起初的
    更新
    {{$index}}
    {{data.original}}
    {{data.updated}
    var-app=angular.module(“plunker”,[]);
    app.controller(“MainCtrl”、[“$scope”、函数($scope){
    $scope.textBoxGroups=[{
    textBoxOne:{值:'},
    textBoxTwo:{值:“”
    } ];
    $scope.add=函数(){
    $scope.textBoxGroups.push({
    textBoxOne:{值:'},
    textBoxTwo:{值:“”
    });
    }
    $scope.remove=函数($index){
    //只需传入要删除的元素的$index
    //然后把它拼接起来
    $scope.textBoxGroups.splice($index,1);
    } 
    $scope.doSomething=函数(){
    $scope.processedData=[];
    $scope.charCount=0;
    angular.forEach($scope.textBoxGroups,函数(textBoxGroup){
    $scope.processedData.push({
    原件:textBoxGroup.textBoxOne.value,
    更新:textBoxGroup.textBoxOne.value.toUpperCase()
    });
    $scope.charCount+=textBoxGroup.textBoxOne.value.length;
    });
    }
    }]);
    
    实际问题是什么?我的意思是我将文本框值传递给一个函数..我们可以生成多个文本框。函数每次都会返回数组,返回第一行文本框的值,然后返回第二行文本框的值,依此类推。马上用小提琴粘贴代码。请您将变量重新命名,以帮助我们理解您的操作
    ng repeat=“x in b”
    就是这么不清楚。而且似乎
    b
    甚至没有在
    $scope
    上定义,所以它不会呈现。完全正确,先生。这是我需要的相同输出,但我需要使用一个函数,我想在函数中发送文本框值,然后我必须对每行值执行一些操作,然后我必须将结果保存在数组索引中,如我的_数组(“sum1”,“sum2”,“sum3”);为什么需要该函数。同步值就在
    textBoxGroup
    变量中。什么叫“发送值”将它们发送到哪里?只需在模型上循环,对每个元素执行所需的任何操作,然后将任何结果放入新数组中。要显示新数组,只需将其定义为
    $scope
    你可以在模板中访问它。实际上,我正在做一个复杂的项目(对我来说很复杂)。如果你不介意在摘要栏中发布该网页链接,每个框的日期和时间计算都会显示出来…这是我工作的需要。我已经更新了答案,以显示你可以做一些事情的一种方式
          <head>
            <script>document.write('<base href="' + document.location + '" />');</script>
            <link rel="stylesheet" href="style.css" />
            <script data-require="angular.js@1.0.x" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js" data-semver="1.0.8"></script>
    
          </head>
          <body><div ng-app="plunker" ng-controller="MainCtrl">
    
               <div ng-repeat='selecting in selects'  ng-init="selecting.var1=selecting.var2=a='';"> 
    
            <input type="text" ng-model="selecting.var1">
            <input type="text" ng-model="selecting.var2">
    
          <button type='button' ng-click='remove(selecting)'>Remove</button>
          </div>
          <div>
            <button type='button' ng-click='add()'>Add</button>
           </div>
           <p>{{ x = test()}}</p>
          <!-- first block -->
          <!-- second block and so on--> 
          <li ng-repeat="x in b" style="list-style:none">
         first attempt=  {{ x.first  | number:0  }} <br>
         second second= {{ x.second | number:0}}
          </li>
          </div>
    
          <script>
             var app = angular.module("plunker",[]);
             app.controller("MainCtrl",['$scope',function($scope){
    
    
                  $scope.test = function()
                  {
                         var result=1,id2,a,b;
                         $scope.result1 = [{}];
    
                             angular.forEach($scope.selects, function(value)
                             { // loop over array to process all items
                                a = value.var1;
                                b = value.var2;
                                alert(a);
    
                            $scope.result1.push({name:$scope.selecting.var1});
                                //myarray = {"first":a,"second":b}
                                //a['first','second'] = (a,b);
                                    return result1;
                            })
                  }
                  $scope.selects = [{}]; // default 1 sets
                // functions to ADD/Remove --------------------------------------------------------------------------------
                  $scope.add = function() 
                  {
                            $scope.selects.push({});
                  }
                  $scope.remove = function(item) 
                  {
                            angular.forEach($scope.selects, function(value, key) 
                            {
                                if (value == item) 
                                {
                                    $scope.selects.splice(key, 1);
                                }
                            });
                 }
                // functions to ADD/Remove ---------------------------------------------------------------------------------
    
            }]);
          </script>
          </body>
    
        </html>
    
    <!DOCTYPE html>
    <html ng-app="plunker">
    
    <head>
    
      <script>document.write('<base href="' + document.location + '" />');</script>
      <link rel="stylesheet" href="style.css" />
      <script data-require="angular.js@1.0.x" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js" data-semver="1.0.8"></script>
    
    </head>
    
    <body>
    
      <div ng-app="plunker" ng-controller="MainCtrl">
    
        <div ng-repeat='textBoxes in textBoxGroups' > 
    
          <input type="text" ng-model="textBoxes.textBoxOne.value">
          <input type="text" ng-model="textBoxes.textBoxTwo.value">
    
          <button type='button' ng-click='remove($index)'>Remove</button>
    
        </div>
    
        <div>
          <button type='button' ng-click='add()'>Add</button>
        </div>
    
        <div>
          <button type='button' ng-click=doSomething()>Do Something</button>
        </div>
    
        <h3>Text Box Group Model</h3>
    
        <table>
          <thead>
            <tr>
              <th>$index</th>
              <th>TxtBox1</th>
              <th>TxtBox2</th>
            </tr>
          </thead>
          <tbody>
            <tr ng-repeat='textBoxes in textBoxGroups'>
              <td>{{$index}}</td>
              <td>{{textBoxes.textBoxOne.value}}</td>
              <td>{{textBoxes.textBoxTwo.value}}</td>
            </tr>
          </tbody>
        </table>
    
         <h3>Char Count: {{charCount}}</h3>
    
          <h3 ng-show="processedData.length">Data After Some Process</h3>
    
    
        <table ng-show="processedData.length">
          <thead>
            <tr>
              <th>$index</th>
              <th>Original</th>
              <th>Updated</th>
            </tr>
          </thead>
          <tbody>
            <tr ng-repeat='data in processedData'>
              <td>{{$index}}</td>
              <td>{{data.original}}</td>
              <td>{{data.updated}}</td>
            </tr>
          </tbody>
        </table>
    
    
      </div>
    
    
    
    
    <script>
    
    var app = angular.module("plunker",[]);
    
    app.controller("MainCtrl", ['$scope', function($scope){
    
    
    
    
      $scope.textBoxGroups = [ {
    
          textBoxOne: { value: '' },
          textBoxTwo: { value: '' }
    
        } ];
    
    
       $scope.add = function(){
        $scope.textBoxGroups.push({
    
          textBoxOne: { value: '' },
          textBoxTwo: { value: '' }
    
        });
      }
    
      $scope.remove = function($index){
    
        // just pass in the $index of the element you want to remove
        // and splice it out
    
        $scope.textBoxGroups.splice($index, 1);
    
      } 
    
      $scope.doSomething = function(){
    
        $scope.processedData = [];
        $scope.charCount = 0;
    
        angular.forEach($scope.textBoxGroups, function(textBoxGroup){
          $scope.processedData.push({
            original: textBoxGroup.textBoxOne.value,
            updated: textBoxGroup.textBoxOne.value.toUpperCase()
          });
    
          $scope.charCount += textBoxGroup.textBoxOne.value.length;
        });
    
    
    
      }
    
    }]);
    
    </script>
    
    </body>
    
    </html>