AngularJS范围值未定义但API工作正常

时间:2017-03-21 10:38:13

标签: javascript angularjs

我没有收到任何错误消息,但是在调试Scope值没有绑定时,它显示Undefined ...

控制器:

$scope.companyModify = function () {
    var param = {
        companyId:$scope.companyId,
        companyName:$scope.companyName,
        billPrintLinesTop:$scope.billPrintLinesTop,
        billPrintLinesBottom:$scope.billPrintLinesBottom,
        isPrintHeader:$scope.isprintHeader,
        billTypeId:$scope.billTypeId,
        billColumnId :$scope.billColumnId,
        noOfCopies: $scope.noOfCopies,
        billHeaderAlignmentId: $scope.billHeaderAlignmentId,
        billTitle: $scope.billTitle,
        billSortOrderId:$scope.billSortOrderId,
        posDefaultQty:$scope.posDefaultQty,
        posTaxTypeId:$scope.posTaxTypeId,
        isAllowNegativeStock:$scope.isAllowNegativeStock,
        serviceTaxCalcTypeId : $scope.serviceTaxCalcTypeId,
        wishMessage:$scope.wishMessage,
        coinageBy:$scope.coinageBy,
        isAutoGenerateProductCode:$scope.isAutoGenerateProductCode
    };

    console.log(param);

致电公司修改功能: companyModify的开括号在SocketService中关闭......

   SocketService.post(apiManage.apiList['CompanyModify'].api,param).
     then(function (resp) {
       var data = resp.data.response;

       if (data.status === true) {
            angular.forEach($scope.companyList, function (value) {
                 if (value.companyId == $scope.companyId) {
                    value.$edit = false;
                 }
        });

            Notify.alert(data.message, {
                status: 'success',
                pos: 'top-right',
                timeout: 5000
            });
            $scope.load();

        }
        else {
            Notify.alert(data.message, {
                status: 'danger',
                pos: 'top-right',
                timeout: 5000
            });
        }
    });
};

1 个答案:

答案 0 :(得分:0)

确保$ scope正确地注入控制器

someModule.controller('MyController', ['$scope', function($scope) {
  ...
  $scope.aMethod = function() {
  ...
  }
  ...
}]);

在您的html代码中,输入值绑定到company.XXX

<input type="text" class="form-control input-sm" ng-model="company.posTaxTypeId" placeholder="Enter POSTaxCalculation"  >

如果您希望代码可以使用控制器,则必须使用相同的绑定并使用posTaxTypeId: $scope.company.posTaxTypeId代替posTaxTypeId: $scope.posTaxTypeId

或将您的HTML代码更改为:

<span data-ng-show="!company.isEdit" data-ng-bind="posTaxTypeId"></span>
<span data-ng-show="company.isEdit"> 
  <input type="text" class="form-control input-sm" ng-model="posTaxTypeId" placeholder="Enter POSTaxCalculation"  >
</span>

还确保正确声明绑定,不带空格:

<强>坏

data-ng-bind="company.posTaxTypeId "
ng-model="company.posTaxTypeId "

不可

data-ng-bind="company.posTaxTypeId"
ng-model="company.posTaxTypeId"