AngularJs + TypeScript: - 如何将变量的值赋给textfield

时间:2015-07-17 13:20:33

标签: angularjs angularjs-scope typescript typescript1.4 typescript1.5

我已经采取了两个div一个是左对齐,第二个是右。在这两个div我有一个文本框。我给了一个ng-href,我试图将左div的数据复制到右div但它&# 39;不要这样做。它什么都不做。 这是我的HTML代码: -

<div data-ng-app="CustomerNew" data-ng-controller="CreateCustomerCtrl as custom" data-ng-init="getFormData();data();">
    <div id="div1" align="left">
    <tr>
     <td style="width: 200px">First Name:</td>
    <td>
    <asp:TextBox ID="txtFirstName" TabIndex="1" runat="server" CssClass="NormalTextBox"Width="160px" Height="10px" Name="txtFirstName" ng-model="custom.txtFirstName"></asp:TextBox>
    </td>
    </tr>
    </div>

<div id="Div2" align="Right">
<tr>
<td colspan="2" width="100">
<a ng-href="" Width="147px" style="cursor:pointer;" TabIndex="12" 
ng-click="ButtonCopy(custom.txtFirstName)">Copy Contact Info</a>
</td>
</tr>
 <tr>
<td style="width: 200px">First Name:</td>
<td>
<asp:TextBox ID="txtFirstNameBilling" TabIndex="12" runat="server" CssClass="NormalTextBox"Width="160px" Height="10px" ng-model="custom.txtFirstNameBilling"></asp:TextBox>
</td>
</tr>
</div>

赋值: -

    /// <reference path="../interface/interface.ts" />
    /// <reference path="../../scripts/typings/jquery/jquery.d.ts" />
    /// <reference path="../../scripts/typings/angularjs/angular.d.ts" />

    module CustomerNew.controllers {
        export class CreateCustomerCtrl {
            static $inject = ['$scope', '$http', '$templateCache'];
            debugger;
            constructor(protected $scope: ICustomerScope,
                protected $http: ng.IHttpService,
                protected $templateCache: ng.ITemplateCacheService) {
                $scope.ButtonCopy = this.ButtonCopy;
            }
     public ButtonCopy = (FirstName) => {
this.$scope.txtFirstNameBilling = FirstName;
            }
            //end
        }
        var customerapp = angular.module("CustomerNew", []);
        customerapp.controller('CreateCustomerCtrl', CustomerNew.controllers.CreateCustomerCtrl);
    }

我的界面

module CustomerNew {
    export interface ICustomerScope extends ng.IScope {
        ButtonCopy: (FirstName) => void;
    }
}

我也尝试过Dean Ward所建议的不同方式,但是出现了新的问题

Error Image 更新了错误2

![在此输入图片说明] [2]

1 个答案:

答案 0 :(得分:1)

您正在使用controllerAs语法。在这种情况下,你不应该使用范围:

public ButtonCopy = (FirstName) => {
    this.txtFirstNameBilling = FirstName;
}

您应该使用ng-click前缀

指定custom
<a ng-href="" Width="147px" style="cursor:pointer;" TabIndex="12" 

ng-click =&#34; custom.ButtonCopy(custom.txtFirstName)&#34;&gt;复制联系信息

示例摘录:

&#13;
&#13;
var customerapp = angular.module("CustomerNew", []);
customerapp.controller('CreateCustomerCtrl', [
  "$scope", "$http", "$templateCache", 
  function($scope, $http, $templateCache) {
    this.ButtonCopy = function(FirstName) {
      console.log("Boom");
      this.txtFirstNameBilling = FirstName;
    }
  }
]);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div data-ng-app="CustomerNew" data-ng-controller="CreateCustomerCtrl as custom">
    <div id="div1" align="left">
    <tr>
     <td style="width: 200px">First Name:</td>
    <td>
    <input ng-model="custom.txtFirstName" />
    </td>
    </tr>
    </div>
<div id="Div2" align="Right">
<tr>
<td colspan="2" width="100">
  <button ng-click="custom.ButtonCopy(custom.txtFirstName)">Copy Contact Info</button>
</td>
</tr>
 <tr>
<td style="width: 200px">First Name:</td>
<td>
<input ng-model="custom.txtFirstNameBilling">
</td>
</tr>
</div>
&#13;
&#13;
&#13;

您后续的TypeScript问题与用于控制器的类的未定义字段有关。在这种情况下,如下更改类定义将修复它:

export class CreateCustomerCtrl {
    static $inject = ['$http', '$templateCache'];
        constructor(
            protected $http: ng.IHttpService,
            protected $templateCache: ng.ITemplateCacheService) {
        }

        public txtFirstNameBilling: string;
        public txtFirstName: string;
        public ButtonCopy = (FirstName) => {
            this.txtFirstNameBilling = FirstName;
        }
    }