引入了controller,为什么一直报错Uncaught Error: [$injector:nomod]


初衷是要实现一个三级联动
正常注入了controller,仍然报错

clipboard.png

将controller.js 改为angular.module('ngShowcaseApp',[])
页面不显示数据

clipboard.png


 <!DOCTYPE html>
<html 
ead>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewprot" content="width=device-width">
    <title>Document</title>
    <link rel="stylesheet" href="http://apps.bdimg.com/libs/todc-bootstrap/3.1.1-3.2.1/todc-bootstrap.min.css">
</head>
<body ng-app="ngShowcaseApp" ng-controller="ctrl.select.cascade">
   <div class="row">
          <div class="col-md-4">
              <select class="form-contrl" ng-model="vm.country" ng-options="country.label for country in vm.country">
                  <option value="">--请选择国家--</option>
              </select>
          </div>
          <div class="col-md-4" ng-if="vm.country.provinces">
              <select class="form-contrl" ng-model="vm.province" ng-options="province.label for value in vm.provinces">
                  <option value="">--请选择省份--</option>
              </select>
          </div>
          <div class="col-md-4" ng-if="vm.province.cities">
              <select class="form-contrl" ng-model="vm.city" ng-options="city.label for value in vm.cities">
                 <option value="">--请选择城市--</option>
              </select>
          </div>
   </div>
   <div>您选择的是:{{country.label}} -- {{province.label}} -- {{city.label}}</div>

   <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script src="http://apps.bdimg.com/libs/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <script src="http://apps.bdimg.com/libs/angular.js/1.4.0-beta.4/angular.min.js"></script>    
    <script src="data.controller.js"></script>
    <script src="data.js"></script>
</body>
</html>


 'use strict';

angular.module('ngShowcaseApp',[])
.controller('ctrl.select.cascade',function($scope, CityData) {
    var vm = $scope.vm = {};
    vm.countries = CityData;
    //更换国家时清空省
    $scope.$watch('vm.country',function(country) {
        vm.province = null;
    });
    //更换省的时候清空市
    $scope.$watch('vm.province', function(province) {
        vm.city = null;
    });
});


 'use strict';

angular.module('ngShowcaseApp').constant('CityData', [
  {
    label: '中国',
    flag: 'cn.png',
    provinces: [
      {
        label: '北京',
        cities: [
          {
            label: '朝阳区'
          },
          {
            label: '宣武区'
          },
          {
            label: '海淀区'
          }
        ]
      },
      {
        label: '河北',
        cities: [
          {
            label: '石家庄'
          },
          {
            label: '承德'
          },
          {
            label: '唐山'
          }
        ]
      }
    ]
  },
  {
    label: '美国',
    flag: 'us.png',
    provinces: [
      {
        label: '纽约',
        cities: [
          {
            label: '曼哈顿区'
          },
          {
            label: '皇后区'
          }
        ]
      },
      {
        label: '德克萨斯州',
        cities: [
          {
            label: '休斯顿'
          },
          {
            label: '达拉斯'
          }
        ]
      },
      {
        label: '加利福尼亚州'
      }
    ]
  }
]);

web前端开发 HTML angularjs JavaScript

其实我是你父亲 9 years, 4 months ago

页面下拉列表的数据是从controller里面的vm塞进去的,三个列表最好通过三个变量控制:


 country
province
city

当country变化的时候(通过 $watch(country) )设置province列表的数值,同理设置city列表

DEMO


另外,关于你提到的报错怀疑是不是javascrpt语法不对,


 angular.module('ngShowcaseApp',[])

注意那个逗号。

會社員小草莓 answered 9 years, 4 months ago

没创建module?

angular.module('ngShowcaseApp',[]). controller(...)

July20 answered 9 years, 4 months ago

Your Answer