无法阅读'的财产'为null

时间:2014-09-10 02:02:34

标签: angularjs

我正试图从模态中发帖。我收到此错误消息

  

无法读取属性'CustomerPhoneNumber'为null

删除selectedCustomer后,一切正常。部分ng模型。我需要让它按原样运行。不知道如何让它读取输入值。

 //Post New Customer
$scope.submitCustomer = function () {
   var data = {
        CustomerId: $scope.selectedCustomer.CustomerId,
        CustomerPhoneNumber: $scope.selectedCustomer.CustomerPhoneNumber,
        CustomerName: $scope.selectedCustomer.CustomerName,
        CustomerFaxNumber: $scope.selectedCustomer.CustomerFaxNumber,
        CustomerAddress: $scope.selectedCustomer.CustomerAddress,
        CustomerCity: $scope.selectedCustomer.CustomerCity,
        CustomerState: $scope.selectedCustomer.CustomerState,
        CustomerZipcode: $scope.selectedCustomer.CustomerZipcode,
        CustomerWebsite: $scope.selectedCustomer.CustomerWebsite,
        CustomerOtherShit: $scope.selectedCustomer.CustomerOtherShit,
        CustomerHidden: $scope.selectedCustomer.CustomerHidden,
        CustomerPM: $scope.selectedCustomer.CustomerPM,
        CustomerAdmin: $scope.selectedCustomer.CustomerAdmin,
        CustomerAccountant: $scope.selectedCustomer.CustomerAccountant,
        CustomerSuperintendent: $scope.selectedCustomer.CustomerSuperintendent

    }
    $http.post('/api/apiCustomer/PostNewCustomer', data).success(function (data, status, headers) {
        console.log(data); window.top.location.reload();
    });

};

 <form ng-submit="submitCustomer()" enctype="multipart/form-data">

                        <tabset>
                            <tab heading="CustomerList">
                                <div class="col-xs-12" style="margin-top:5px">
                                    <div style="overflow: auto;height:200px" id="scrollAreaCustomers">
                                        <table class="table">
                                            <tr>
                                                <th style="font-weight: bold;">Name</th>
                                            </tr>
                                            <tr><input type="text" placeholder="New Customer" ng-model="selectedCustomer.CustomerName" ng-enter="data.static = true" /></tr>
                                            <tr ng-repeat="customer in customerArray | filter:searchCustomerName" class="pointer">
                                                <td ng-click="setSelectedCustomer(customer);data.static = true">{{customer.CustomerName}}</td>
                                            </tr>
                                        </table>
                                    </div>
                                </div><!--End col-xs-12-->

                            </tab><!--End CustomerList Tab-->
                            <tab heading="CustomerDetails" active="data.static">
                                <div class="col-xs-12" style="margin-top:5px">
                                    <div class="inline-fields">
                                        <label>Name:</label>
                                        <input style="width:150px;margin-left:19px" type="text" ng-model="selectedCustomer.CustomerName" />
                                        <label style="margin-left: 80px">Website:</label>
                                        <input style="width:150px" type="text" ng-model="selectedCustomer.CustomerWebsite" />
                                    </div>
                                    <div class="inline-fields">
                                        <label style="margin-left:1px;">Address:</label>
                                        <input style="width:180px" ng-model="selectedCustomer.CustomerAddress" type="text">
                                    </div>
                                    <div class="inline-fields">
                                        <label style="margin-left: 30px">City:</label>
                                        <input style="width:80px;" ng-model="selectedCustomer.CustomerCity" type="text">

                                        <label>State:</label>
                                        <input style="width:30px" ng-model="selectedCustomer.CustomerState" type="text">

                                        <label>Zipcode:</label>
                                        <input style="width:50px" ng-model="selectedCustomer.CustomerZipcode" type="text">
                                    </div>
                                    <div class="inline-fields">
                                        <label style="margin-left: 14px">Phone:</label>
                                        <input style="width:121px;" ng-model="selectedCustomer.CustomerPhoneNumber" type="text">

                                        <label>Fax:</label>
                                        <input style="width:115px" ng-model="selectedCustomer.CustomerFaxNumber" type="text">

                                    </div>

                                </div><!--End col-xs-12-->
                            </tab>

                            <tab heading="CustomerEmployees">
                                <div class="col-xs-12" style="margin-top:5px">
                                    <div class="inline-fields" style="">
                                        <label style="margin-left:49px">PM:</label>
                                        <input style="width:150px" ng-model="selectedCustomer.CustomerPM" type="text">
                                    </div>
                                    <div class="inline-fields" style="">
                                        <label style="margin-left:26px">Admin:</label>
                                        <input style="width:150px" ng-model="selectedCustomer.CustomerAdmin" type="text">
                                    </div>
                                    <div class="inline-fields" style="">
                                        <label style="margin-left:1px">Acountant:</label>
                                        <input style="width:150px" ng-model="selectedCustomer.CustomerAccountant" type="text">
                                    </div>
                                    <div class="inline-fields" style="">
                                        <label style="margin-left:30px">Super:</label>
                                        <input style="width:150px" ng-model="selectedCustomer.CustomerSuperintendent" type="text">
                                    </div>
                                </div>
                            </tab>
                        </tabset><!--End Tab Content-->
                        <div class="col-xs-12">
                            <input style="margin-left:360px;margin-top:25px;width:70px" type="submit" value="Save" go-click="#" />
                            <input style="margin-left: 20px; width: 70px; margin-top: 25px" type="button" data-dismiss="modal" value="Exit" go-click="#" />
                        </div>
                    </form>

更新

TypeError: Cannot read property 'CustomerId' of null
at Scope.$scope.submitCustomer (http://localhost:44301/MyScripts/JobController.js:255:48)
at http://localhost:44301/Scripts/angular.js:10836:21
at http://localhost:44301/Scripts/angular.js:19094:17
at Scope.$eval (http://localhost:44301/Scripts/angular.js:12673:28)
at Scope.$apply (http://localhost:44301/Scripts/angular.js:12771:23)
at HTMLFormElement.<anonymous> (http://localhost:44301/Scripts/angular.js:19093:21)
at HTMLFormElement.x.event.dispatch (http://localhost:44301/Scripts/jquery-1.10.2.min.js:22:14129)
at HTMLFormElement.v.handle (http://localhost:44301/Scripts/jquery-1.10.2.min.js:22:10873) angular.js:10061(anonymous function) angular.js:10061(anonymous function) angular.js:7357Scope.$apply angular.js:12773(anonymous function) angular.js:19093x.event.dispatch jquery-1.10.2.js:9789v.handle

1 个答案:

答案 0 :(得分:1)

好的,这是你的问题:

  $scope.customers = [
    {CustomerName: 'PizzaHut', CustomerAddress: '1234 Street', CustomerWebsite: 'website.com', CustomerCity: 'Houston', CustomerState: 'TX', CustomerZipcode: '77493',CustomerPhoneNumber: '713-987-5632', CustomerFaxNumber: '281-789-2145'}, 
    {CustomerName: 'PappaJohns', CustomerAddress: '9876 Hayes', CustomerWebsite: 'widget.com', CustomerCity: 'Katy', CustomerState: 'TX', CustomerZipcode: '77042',CustomerPhoneNumber: '281-452-8523', CustomerFaxNumber: '713-565-9657'}
    ];
  $scope.customerArray = [];
  $scope.selectedCustomer = $scope.customerArray[0];

你有将你的客户添加到的scope.customers,以及只是一个空数组的scope.customerArray。

当您选择客户时,您正在选择空数组的第一个成员。

所以改变这个:

  $scope.selectedCustomer = $scope.customerArray[0];

到此:

  $scope.selectedCustomer = $scope.customers[0];

修改

好的,我看到了您的更新,但您仍然遇到了大致相同的问题。

  $scope.customerArray = [
    {CustomerName: 'PizzaHut', CustomerAddress: '1234 Street', CustomerWebsite: 'website.com', CustomerCity: 'Houston', CustomerState: 'TX', CustomerZipcode: '77493',CustomerPhoneNumber: '713-987-5632', CustomerFaxNumber: '281-789-2145'}, 
    {CustomerName: 'PappaJohns', CustomerAddress: '9876 Hayes', CustomerWebsite: 'widget.com', CustomerCity: 'Katy', CustomerState: 'TX', CustomerZipcode: '77042',CustomerPhoneNumber: '281-452-8523', CustomerFaxNumber: '713-565-9657'}
    ];
  $scope.customerArray = [];

使用数据初始化数组,然后将其重新初始化为空数组。您将selectedCustomer设置为空数组的第一个成员。删除第二行,你应该是金色的。