簡體   English   中英

無法在代碼中使用Angular 1.4.7

[英]Can't use Angular 1.4.7 in Code

我有以下代碼:

<!DOCTYPE html>
<html>
<head>
<title>Lists Test</title>
<script src="https://code.angularjs.org/1.1.1/angular.min.js"></script>
<script>
    function Controller($scope) {

        $scope.backupCountries = {
            "id": "field10",
            "items": [{
                "id": "10",
                "StateGroupID": "0",
                "name": "United State"
    }, {
                "id": "2",
                "StateGroupID": "1",
                "name": "Canada"
    }]
        };

        $scope.backupStates = {
            "id": "field20",
            "StateGroups": [{
                    "items": [{
                        "id": "1",
                        "name": "Alabama"
        }, {
                        "id": "2",
                        "name": "Alaska"
        }, {
                        "id": "3",
                        "name": "Arizona"
        }, {
                        "id": "4",
                        "name": "California"
        }]
    },

                {
                    "items": [{
                        "id": "201",
                        "name": "Alberta"
        }, {
                        "id": "202",
                        "name": "British Columbia"
        }, {
                        "id": "303",
                        "name": "Manitoba"
        }, {
                        "id": "304",
                        "name": "Ontario"
        }]
    }]
        };

        $scope.Countries = $scope.backupCountries;
        $scope.getStates = function () {
            console.log($scope.selectedCountry);
            return         $scope.backupStates.StateGroups[$scope.selectedCountry].items;
        };
        //$scope.currentStates = $scope.backupStates.StateGroups[0];

        /*$scope.$watch('currentStates', function(value, oldValue){
            //alert(value);
            //alert(JSON.stringify(value));
            //$scope.currentStates = (value == "10") ?  States.StateGroups[0] : States.StateGroups[1];
        });*/
    };
</script>
</head>
<body>
<div ng-app ng-controller="Controller">
     <h2 class="page-title">Model</h2>
           <div class="row-fluid">
            <div class="span4">
                <label for="cboGroup">Countries</label>
                <select data-ng-model="selectedCountry">
                        <option value="">Please Select a Country</option>
                      <option ng-repeat='country in Countries.items'           value='{{country.StateGroupID}}'>{{country.name}}</option>
                </select>
            </div>

            <div class="span4">
            <label for="cboItem">States</label>
            <select data-ng-model="selectedState">
                <option value="">Please select a state</option>
                <option ng-repeat='state in getStates()'>{{state.name}}</option>
            </select>
        </div>

        <div class="well">what I am trying to archive is that the items are changing each time the group changes.</div>
        <div>Countries : {{Countries.items | json}}</div>
        <div>States : {{getStates()}}</div>
    </div>

我一直在努力的將這段代碼遷移到Angular 1.4中,但是我不知道我的代碼有什么問題或更改了哪些內容。 該代碼在Angular 1.1.1中可以正常工作,但是當我將Angular源更改為具有較高版本的js時,所有代碼都變黑了。

一些奇怪的事情脫穎而出:

  1. 您可以使用不帶data- *前綴的ng-model。

  2. $ scope.selectedCountry不會在任何地方初始化。

  3. 需要使用ng-app指令連接您的應用程序:ng-app =“ my_app” id =“ ng-app”

  4. 您沒有模塊或控制器定義

我的建議是弄清楚哪個版本的AngularJS開始破壞您的代碼,以及您的代碼仍然可以使用的最新版本。 然后閱讀發行文檔,查看所做的更改可能會破壞您的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM