我的角度方法不会发射

时间:2017-12-11 09:43:01

标签: javascript c# angularjs json model-view-controller

这是我第一次使用棱角分明,而且我是这个领域的新手;这些方法根本不会触发,并且不会发生样式的变化。我几次检查了一切,但我找不到我的错误。请帮帮我。提前谢谢。

这是角度代码:

onResume()

这是我的Html:

<script language="javascript">


    function CustomerPresenter($scope, $http) {

        $scope.Customer = {

            "CustomerName": "",
            "CustomerCode": "",
            "CustomerAmount": "",
            "CustomerAmountColor": ""
        };
        alert($scope.Customer);
        $scope.Customers = {};
        alert($scope.Customers)
        $scope.$watch("Customers", function () {

            for (var i = 0; i < $scope.Customers.length; i++) {
                alert(i);
                var cust = $scope.Customers[i];
                cust.CustomerAmountColor = $scope.getColor(cust.CustomerAmount);
            }

        });

        $scope.getColor = function (Amount) {

            if (Amount == 0) {
                return "";

            }

            else if (Amount > 100) {

                return "Blue";

            }
            else {
                return "Red";
            }
            alert(Amount);
        }

        $scope.$watch("Customer.CustomerAmount", function () {

            $scope.Customer.CustomerAmountColor = $scope.
                getColor($scope.Customer.CustomerAmount);
            alert(getColor);

        });


        $scope.Add = function () {

            $http({ method: "POST", data: $scope.Customer, url: "Submit" }).
                success(function (data, status, headers, config) {

                    $scope.Customers = data;
                    alert(data);
                    $scope.Customer = {
                        "CustomerName": "",
                        "CustomerCode": "",
                        "CustomerAmount": "",
                        "CustomerAmountColor": ""
                    };
                });

        }

        $scope.Load = function () {
            $http({ method: "GET", url: "GetCustomerJSON" }).
                success(function (data, status, headers, config) {

                    $scope.Customers = data;
                    alert($scope.Customers);

                });
        }

        $scope.Load();

    }
    var app = angular.module("myApp", []);
    app.controller('CustomerPresenter', CustomerPresenter);
</script>

和控制器中的方法是:

<body ng-app="myApp">
<div>

    <div ng-controller="CustomerPresenter">
        <form id="frm">
            <i> Customer Name :</i> <input name="customer.CustomerName" ng-model="Customer.CustomerName" id="txtCustomerName" type="text" />
            <br />
            @Html.ValidationMessageFor(z => z.customer.CustomerName)
            <br />
            <i> Customer Code :</i><input name="customer.CustomerCode" ng-model="Customer.CustomerCode" id="txtCustomerCode" type="text" />
            <br />
            <br />
            @Html.ValidationMessageFor(z => z.customer.CustomerCode)
            <br />
            <i>Customer Amount :</i><input name="customer.CustomerAmount" style="background-color:{{ Customer.CustomerAmountColor }}" ng-model="Customer.CustomerAmount" id="txtCustomerAmount" type="text" />

        </form>

        <br />
        <br />
        <input id="btn" type="button" value="add customer" ng-click="Add()" />

        <br />
        <div id="status" style="width:200px;height:20px"></div>
        <table id="tblCustomer">
            <tr>
                <td>Customer Name</td>
                <td>Customer Code</td>
                <td>Customer Amount</td>
            </tr>
            <tr ng-repeat="cust in Customers">
                <td>{{cust.CustomerName}}</td>
                <td>{{cust.CustomerCode}}</td>
                <td style="background-color: {{ cust.CustomerAmountColor }}">{{cust.CustomerAmount}}</td>
            </tr>
        </table>

    </div>
</div>

0 个答案:

没有答案