首頁  >  文章  >  後端開發  >  angular ng-model 綁定的select不能傳回?

angular ng-model 綁定的select不能傳回?

WBOY
WBOY原創
2016-09-28 08:54:111035瀏覽

我透過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>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn