Rumah >pembangunan bahagian belakang >tutorial php >angular ng-model 绑定的select不能传回?

angular ng-model 绑定的select不能传回?

WBOY
WBOYasal
2016-09-28 08:54:111064semak imbas

我通过json生成了多级select

<code><div ng-app="xx" ng-controller="eee">
   <div>
                   

 **{{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>
                   
                </div>
</div></code>

app.js是这样的:

<code> 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)

            }
           


        });</code>

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

回复内容:

我通过json生成了多级select

<code><div ng-app="xx" ng-controller="eee">
   <div>
                   

 **{{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>
                   
                </div>
</div></code>

app.js是这样的:

<code> 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)

            }
           


        });</code>

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

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

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

<code>        **$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)

        }
       
</code>
<code>    });
    要定义一个$scope.col_selectlv,才能够获取到col_selectlv</code>
Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn