将字符串附加到作用域,并将值分配给它

时间:2019-05-06 19:57:10

标签: angularjs html5

如何将字符串附加到作用域,然后在angularjs中为其分配值,然后在html5中显示作用域?

示例: HTML标记:

<table>
   <tr>
      <td>Transitoria:</td>
      <td>
         <input type="text" ng-model="formData.transitoryAccount"
                ng-keydown="$event.keyCode === 13 && searchGLAccount(formData.transitoryAccount , 'transitoryAccount')">
      </td>
      <td>{{transitoryAccount.label}}</td>
   </tr>
   <tr>
      <td>Error Account:</td>
      <td>
         <input type="text" ng-model="formData.errorAccount"
                ng-keydown="$event.keyCode === 13 && searchGLAccount(formData.errorAccount, 'errorAccount')">
      </td>
      <td>{{errorAccount.label}}</td>
   </tr>
   <tr>
      <td>End of Year Profit:</td>
      <td>
         <input type="text" ng-model="formData.EOYProfitAccount"
                ng-keydown="$event.keyCode === 13 && searchGLAccount(formData.EOYProfitAccount, 'EOYProfitAccount')">
      </td>
      <td>{{EOYProfitAccount.label}}</td>
   </tr>
</table>

在控制器中

scope.searchGLAccount = function(srchText, modelName) {
    resourceFactory.runReportsResource.get({
        reportSource: 'getAccountByAccountNo',
        R_accountNo: srchText,
        genericResultSet: false
    }, function (data) {
        scope.glAccount = data[0];
        scope.modelName['.label'] = scope.glAccount.entityName;
        scope.formData.modelName = scope.glAccount.entityAccountNo;
    });
};

我们非常感谢您的协助。.谢谢

3 个答案:

答案 0 :(得分:0)

<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
   
    <script>
    var app = angular.module('test', []);
    
    app.controller('mine',function($scope){
     $scope.transitoryAccount= {label:"Transitoria - Label"};
     $scope.errorAccount= {label:"Transitoria - Label"};
     $scope.EOYProfitAccount= {label:"Transitoria - Label"};
    });
    
    </script>
  </head>

  <body ng-app="test">
    <table ng-controller="mine">
   <tr>
      <td>Transitoria:</td>
      <td>
         <input type="text" ng-model="formData.transitoryAccount" ng-keydown="$event.keyCode === 13 && searchGLAccount(formData.transitoryAccount , 'transitoryAccount')">
      </td>
      <td>{{transitoryAccount.label}}</td>
   </tr>
   <tr>
      <td>Error Account:</td>
      <td>
         <input type="text" ng-model="formData.errorAccount" ng-keydown="$event.keyCode === 13 && searchGLAccount(formData.errorAccount, 'errorAccount')">
      </td>
      <td>{{errorAccount.label}}</td>
   </tr>
   <tr>
      <td>End of Year Profit:</td>
      <td>
         <input type="text" ng-model="formData.EOYProfitAccount" ng-keydown="$event.keyCode === 13 && searchGLAccount(formData.EOYProfitAccount, 'EOYProfitAccount')">
      </td>
      <td>{{EOYProfitAccount.label}}</td>
   </tr>
</table>
    
  </body>

</html>

答案 1 :(得分:0)

由于ng-keydown指令使用searchGLAccount参数的字符串调用modelName函数,因此代码需要对该参数使用bracket notation属性访问器:

scope.searchGLAccount = function(srchText, modelName) {
    resourceFactory.runReportsResource.get({
        reportSource: 'getAccountByAccountNo',
        R_accountNo: srchText,
        genericResultSet: false
    }, function (data) {
        scope.glAccount = data[0];
        ̶s̶c̶o̶p̶e̶.̶m̶o̶d̶e̶l̶N̶a̶m̶e̶[̶'̶.̶l̶a̶b̶e̶l̶'̶]̶ ̶=̶ ̶s̶c̶o̶p̶e̶.̶g̶l̶A̶c̶c̶o̶u̶n̶t̶.̶e̶n̶t̶i̶t̶y̶N̶a̶m̶e̶;̶ 
        scope[modelName].label = scope.glAccount.entityName;
        ̶s̶c̶o̶p̶e̶.̶f̶o̶r̶m̶D̶a̶t̶a̶.̶m̶o̶d̶e̶l̶N̶a̶m̶e̶ ̶=̶ ̶s̶c̶o̶p̶e̶.̶g̶l̶A̶c̶c̶o̶u̶n̶t̶.̶e̶n̶t̶i̶t̶y̶A̶c̶c̶o̶u̶n̶t̶N̶o̶;̶
        scope.formData[modelName] = scope.glAccount.entityAccountNo;
    });
};

答案 2 :(得分:0)

使用[]括号创建新的动态键modelName到作用域,然后添加标签键将其定义为对象并开始工作。请检查功能

<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
   
    <script>
    var app = angular.module('test', []);
    
    app.controller('mine',function($scope){
     
    $scope.searchGLAccount = function(srchText, modelName) {
          $scope[modelName]={};
          $scope.formData={};
          $scope[modelName]["label"]="test"; //added sample value as dont have this data $scope.glAccount.entityName
          $scope.formData[modelName] = "test1"//$scope.glAccount.entityAccountNo
          console.log($scope.transitoryAccount.label,             $scope.formData.transitoryAccount)
   /* resourceFactory.runReportsResource.get({
        reportSource: 'getAccountByAccountNo',
        R_accountNo: srchText,
        genericResultSet: false
    }, function (data) {
        $scope.glAccount = data[0];
        $scope.modelName['.label'] = $scope.glAccount.entityName;
        $scope.formData.modelName = $scope.glAccount.entityAccountNo;
    });*/
};
});
    
    </script>
  </head>

  <body ng-app="test">
    <table ng-controller="mine">
   <tr>
      <td>Transitoria:</td>
      <td>
         <input type="text" ng-model="formData.transitoryAccount" ng-keydown="$event.keyCode === 13 && searchGLAccount(formData.transitoryAccount , 'transitoryAccount')">
      </td>
      <td>{{transitoryAccount.label}}</td>
   </tr>
   <tr>
      <td>Error Account:</td>
      <td>
         <input type="text" ng-model="formData.errorAccount" ng-keydown="$event.keyCode === 13 && searchGLAccount(formData.errorAccount, 'errorAccount')">
      </td>
      <td>{{errorAccount.label}}</td>
   </tr>
   <tr>
      <td>End of Year Profit:</td>
      <td>
         <input type="text" ng-model="formData.EOYProfitAccount" ng-keydown="$event.keyCode === 13 && searchGLAccount(formData.EOYProfitAccount, 'EOYProfitAccount')">
      </td>
      <td>{{EOYProfitAccount.label}}</td>
   </tr>
</table>
    
  </body>

</html>