php - angular ng-model 绑定的select不能传回?
迷茫
迷茫 2017-04-11 10:31:52
[PHP讨论组]

我通过json生成了多级select

<p ng-app="xx" ng-controller="eee">
   <p>
                   

 **{{col_selectlv[1].colname}}**//这个地方下文说到!

                    <select ng-model="col_selectlv[0]" ng-options="x.colname for x in col_select[0]" required>

                        <option></option>
                    </select>
                    <select ng-model="col_selectlv[1]" ng-options="x.colname for x in col_selectlv[0].children"
                            ng-if="col_selectlv[0].children"
                            required >
                        <option></option>
                    </select>
                    <select ng-model="col_selectlv[2]" ng-options="x.colname for x in col_selectlv[1].children"
                            ng-if="col_selectlv[1].children" required >
                        <option></option>
                    </select>
                    <select ng-model="col_selectlv[3]" ng-options="x.colname for x in col_selectlv[2].children"
                            ng-if="col_selectlv[2].children" required >
                        <option></option>
                    </select>
                   
                </p>
</p>

app.js是这样的:

 app.controller('newseditor', function ($scope, $http,$routeParams) {
           $scope.newsid=$routeParams.id?$routeParams.id:'0';
            $http({
                method: 'GET',
                url: 'datacon/col_select.php',

            }).success(function (response) {
                //    console.log(response);
                if (response)
                    $scope.col_select = response;

            })
            $scope.change_selecter = function (r) {
                $scope.select_range = r;
            }
            $scope.news_submit = function () {
                console.log($scope.col_selectlv)

            }
           


        });

通过点击button激活news_submit,希望的效果是能输出col_selectlv,但是输出的结果是未定义。
ng-model是绑定成功了,因为上面标注的那个{{col_selectlv[1].colname}}是可以输出的,但是不知道为什么就是不能通过$scope.col_selectlv获取。

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回复(2)
黄舟

原来是因为ng-if会创建一个子域,所以
app.controller('newseditor', function ($scope, $http,$routeParams) {

        **$scope.col_selectlv={}**
       $scope.newsid=$routeParams.id?$routeParams.id:'0';
        $http({
            method: 'GET',
            url: 'datacon/col_select.php',

        }).success(function (response) {
            //    console.log(response);
            if (response)
                $scope.col_select = response;

        })
        $scope.change_selecter = function (r) {
            $scope.select_range = r;
        }
        $scope.news_submit = function () {
            console.log($scope.col_selectlv)

        }
       
    });
    要定义一个$scope.col_selectlv,才能够获取到col_selectlv
阿神

你的控制器名字是eee还是newseditor,代码好像不完整

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号