【问题标题】:Pass ng-model as parameter to function将 ng-model 作为参数传递给函数
【发布时间】:2017-08-18 07:28:11
【问题描述】:

您好,我是 Angular 的新手,我想重用一个函数来修改不同的模型。

 <p ng-bind="f1" ></p>
 <p ng-bind="f2" ></p>
 <p ng-bind="f3" ></p>
 <button  ng-click="write()" >Save</button>

例如,我有上面的 html。我希望能够使用write() 来绑定 f1、f2 或 f3 的数据,具体取决于我传递给 write() 的数据。我如何通知写入模型以将数据绑定到?换句话说,我如何使函数可重用。

【问题讨论】:

    标签: html angularjs angularjs-directive angularjs-scope reusability


    【解决方案1】:

    我最终这样做是为了访问我的函数中的特定模型

     <p ng-bind="f1" ></p>
     <p ng-bind="f2" ></p>
     <p ng-bind="f3" ></p>
     <button  ng-click="write("f1")" >Save</button>
    

    我将模型作为字符串传递给我的函数

    $scope.write= function(modelName){
    $scope[modelName] = "Some Value"
    }
    

    这种方式 write() 可以被任何模型使用

    【讨论】:

      【解决方案2】:

      你可以直接把ng-model传给函数,

      <button  ng-click="write(f1)" >Save</button>
      

      在控制器中,

      $scope.write = function(val){
       //do with the value
      }
      

      【讨论】:

      • 我想你误会了。我不需要值我需要对模型的引用,所以我可以使用$scope.val = "something" 而不是$scope.f1 = "something"。我不想拥有 3 个不同的写入函数
      • @flexxxit 我还是不明白你想要实现什么
      【解决方案3】:
      $scope.write = function(val){
       if(val.f1){
      //do watevr u want
      }
      else if(val.f2){
      //if value f2 is not undefined
      }
      else if(val.f3){
      
      }
      
      
      
      
      <p data-ng-model="obejct.f1" ></p><p data-ng-model="obejct.f2" ></p><p data-ng-model="obejct.f3" ></p><button  data-ng-click="write(object)" >Save</button>
      
      }
      

      【讨论】:

        【解决方案4】:

        看到这个

        > js:

        var app = angular.module('plunker', []);
        
        app.controller('MainCtrl', function($scope) {
          $scope.f1 = 'Welcome';
          $scope.f2 = 'to';
          $scope.f3 = 'Bangladesh';
          $scope.write = function(x,y,z){
            console.log(x)
            console.log(y)
            console.log(z)
            $scope[x] = 52464645;
            console.log($scope[x]);
          };
        });
        

        html:

        <!DOCTYPE html>
        <html ng-app="plunker">
        
          <head>
            <meta charset="utf-8" />
            <title>AngularJS Plunker</title>
            <script>document.write('<base href="' + document.location + '" />');</script>
            <link rel="stylesheet" href="style.css" />
            <script data-require="angular.js@1.5.x" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js" data-semver="1.5.11"></script>
            <script src="app.js"></script>
          </head>
        
          <body ng-controller="MainCtrl">
            <p ng-bind="f1" ></p>
         <p ng-bind="f2" ></p>
         <p ng-bind="f3" ></p>
         <button  ng-click="write(f1,f2,f3)" >Save</button>
          </body>
        
        </html>
        

        例子。 https://plnkr.co/edit/drZG4ZWetxOWPSPbxn26

        【讨论】:

          猜你喜欢
          • 2017-08-17
          • 1970-01-01
          • 1970-01-01
          • 2013-01-27
          • 2015-08-27
          • 2021-04-13
          • 1970-01-01
          • 2016-09-03
          • 1970-01-01
          相关资源
          最近更新 更多