js:使用angular的http获取json数据

一,本例中使用amaze的select框架,配合angular显示json数据

框架官网链接:http://amazeui.org/javascript/selected

由于页面的异步刷新导致select配置完成后不再接收angular的数据,导致数据无法显示,这里解决使用setTimeout延迟select框架的配置

完成angular数据的植入

1,使用的json数据:

2,具体实现代码:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <!--引入amaze下拉选框需要的资源-->
    <script type="text/javascript" src="../../public/javascript/chosen/chosen.jquery.min.js"></script>
    <link type="text/css" rel="stylesheet" href="../../public/javascript/chosen/chosen.min.css" />

    <script src="../../public/angular-1.6.5/angular.js"></script>
    <script type="text/javascript" src="../../public/javascript/jquery-1.11.1.min.js"></script>

</head>

<script language="JavaScript">
    var app = angular.module(‘shop‘, []);
    app.controller(‘shopCrl‘, function($scope,$http) {
        //$scope.pron=[{id:1,name:‘成都市‘},{id:2,name:‘彭州市‘},{id:3,name:‘德阳市‘}];
        var coun;
        var strn;

        //使用angular http方法接收数据
        $http({
            method: ‘GET‘,
            url: ‘../../public/site.json‘
        }).then(function successCallback(response) {
            //初始化选择框
            $scope.pron = response.data.pro;
            strn = response.data.pro;
            $scope.selectedItems=$scope.pron[0].proname;
            $scope.strn=strn[0].proargs;
            $scope.selectedItemsStr=$scope.strn[0].strname;
            $scope.coun=$scope.strn[0].strargs;
            coun=strn[0].proargs;
        }, function errorCallback(response) {
        });
        $scope.getpro=function(){
        }
        //选择州级
        $scope.chapro=function(selectedItems){
            strn.forEach(function(val,i,strn){
                //遍历数据查找与表相等的数组
                if(selectedItems==strn[i].proname){
                    $scope.strn = strn[i].proargs;
                    //改变ng-model的值
                    $scope.selectedItemsStr=$scope.strn[0].strname;
                    coun=strn[i].proargs
                }
            })
        };
        //选择市级
        $scope.chastr=function(selectedItemsStr){
            coun.forEach(function(val,i,coun){
                if(selectedItemsStr==coun[i].strname){
                    $scope.coun = coun[i].strargs;
                }
            })

        };
    });

</script>
<body ng-app="shop">
<div ng-controller="shopCrl">
    <div class="frmInputBox frmInputBoxNoTextLimit">
        <div class="selectionGroup">
            <div class="dropDown"  style="width:120px;">
                <!--<select data-placeholder="所在州" class="chosen-select-no-single" ng-click="getpro()" tabindex="9"  ng-model="selectedItems" ng-change="chapro(selectedItems)">-->
                <select data-placeholder="所在州" id="pro" class="" ng-click="getpro()" tabindex="9"  ng-model="selectedItems" ng-change="chapro(selectedItems)">
                    <option value=""></option>
                    <option ng-repeat="item in pron">{{item.proname}}</option>
                </select>
            </div>
        </div>
        <div class="selectionGroup">
            <div class="dropDown"  style="width:120px;">
                <select ng-model="selectedItemsStr" id="str" style="height: 37px;width: 118px" ng-change="chastr(selectedItemsStr)" tabindex="9">
                    <option ng-repeat="item in strn" >{{item.strname}}</option>
                </select>
            </div>
        </div>
        <div class="selectionGroup">
            <div class="dropDown"  style="width:120px;">
                <select style="height: 37px;width: 118px" id="cou"  tabindex="9">
                    <option ng-repeat="item in coun" value="{{item.couid}}">{{item.couname}}</option>
                </select>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/javascript">
    //配置选择框,使用setTimeout延迟加载框架配置,让angular的数据先一步进入
    setTimeout(function() {
        $("#pro").addClass("chosen-select-no-single");
        $("#str").addClass("chosen-select-no-single");
        $("#cou").addClass("chosen-select-no-single");
        /*
         .chosen-select-no-single‘ : {disable_search_threshold:10} option小于10项隐藏搜索框。
         .chosen-select-deselect‘:{allow_single_deselect:true}    设置为 true 时非必选的单选框会显示清除选中项图标
         .chosen-select-no-results‘: {no_results_text:‘Oops, nothing found!‘} 没有搜索到结果的时候显示的内容
         */
        var config = {
            ‘.chosen-select‘: {},
            ‘.chosen-select-deselect‘: {allow_single_deselect: true},
            ‘.chosen-select-no-single‘: {disable_search_threshold: 10},
            ‘.chosen-select-no-results‘: {no_results_text: ‘Oops, nothing found!‘},
            ‘.chosen-select-width‘: {width: "100%"}
        }
        for (var selector in config) {
            $(selector).chosen(config[selector]);
        }
        $(‘.chosen-select‘).chosen();
    },10);
</script>
</html>

原文地址:https://www.cnblogs.com/dybe/p/8317931.html

时间: 2024-08-05 09:55:42

js:使用angular的http获取json数据的相关文章

AngularJS学习笔记(3)——通过Ajax获取JSON数据

通过Ajax获取JSON数据 以我之前写的与用户交互的动态清单列表为例,使用JSON前todo.html代码如下: <!DOCTYPE html> <html ng-app="todoApp"> <head> <meta charset="UTF-8"> <title>TO DO List</title> <link href="./bootstrap/css/bootstrap

Ajax 跨域请求 jsonp获取json数据

遇到Ajax的跨域请求出问题 找了中解决办法如下: 参考内容:http://justcoding.iteye.com/blog/1366102 由于受到浏览器的限制,该方法不允许跨域通信.如果尝试从不同的域请求数据,会出现安全错误.如果能控制数 据驻留的远程服务器并且每个请求都前往同一域,就可以避免这些安全错误.但是,如果仅停留在自己的服务器上,Web 应用程序还有什么用处呢?如果需要从多个第三方服务器收集数据时,又该怎么办? 理解同源策略 同源策略阻止从一个域上加载的脚本获取或操作另一个域上的

jquery用ajax方式从后台获取json数据后如何将内容填充到下拉列表

对于问题从后台获取json数据,将内容填充到下拉列表,代码非常简单,具体过程请看下面代码. 需求:url:链接     par:ID       sel:下拉列表选择器 function BuildSelectBox(url, par, sel) {  $(sel).empty();  $.getJSON(url, { id: par }, function (json, textStatus) {   for (var i = json.length - 1; i >= 0; i--) {  

漂亮的表格样式;jQuery清楚表格所有行;js解析后台传过来的JSON数据;动态生成表格数据

一 : 先看看漂亮的表格 css代码: .mylist { width: auto; height:auto; border:1px solid #accdf4; margin-top:10px; font-family:"宋体"; font-size:12px; color:#155c9f; text-align:center; border-collapse: collapse; } .mylist th { background-color:#d0e4ff; text-align

JQuery UI获取JSON数据

最近在学习用JQuery UI开发一个界面,这是javascript的一个框架,功能强大,界面漂亮. 首先要下载JQuery UI的包,引入里面的文件: <link href="jquery-ui.css" rel="stylesheet"> <script src="external/jquery/jquery.js"></script> <script src="jquery-ui.js&q

IT忍者神龟之jQuery 使用 $.getJSON() 跨域获取 JSON 数据

假设在服务器上有文件 http://test.unmi.cc/json.php 文件,它的内容为: [代码 1] 01 02 03 04 05 06 07 08 09 10 <?php header('Content-type: application/json'); $user = array (     "name"  => "Unmi",     "blog" => "http://unmi.cc" )

ajax获取json数据及实现跨域请求

最近想练习一下ajax获取json数据 , 首先上网找一些在线的可用来测试的接口. -----------------------------------------------------这里是接口分割线 Begin-------------------------------------------------------------------------------------------------- 电商接口 淘宝商品搜索建议: http://suggest.taobao.com/s

跨域获取json数据

这是天气json的数据,这里是链接,json的数据接口  http://m.weather.com.cn/data/101010100.html json的数据格式 {"weatherinfo":{"city":"北京","city_en":"beijing","date_y":"2014年3月4 日","date":"",&q

jQuery异步获取json数据的2种方式

jQuery异步获取json数据有2种方式,一个是$.getJSON方法,一个是$.ajax方法.本篇体验使用这2种方式异步获取json数据,然后追加到页面. 在根目录下创建data.json文件: { "one" : "Hello", "two" : "World" } ■ 通过$.getJSON方法获取json数据 <script src="Scripts/jquery-2.1.1.min.js"