繁体   English   中英

ng-options指令不起作用,无法在angularjs库中找到它

[英]ng-options directive is not working, unable to find it in library of angularjs

我的ng-option指令不起作用,在AngularJS库中找不到。 我尝试了几乎所有有效的来源,但仍然只有此指令不起作用。 甚至我也尝试过angularjs.org网站上的代码。

<html>
<head>
    <title>View</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>

    <script>
        var mymodule = angular.module('mymodule' ,[]);
        mymodule.controller('x' , function($scope){
            $scope.point = ['point1' , 'point2' , 'point3' , 'point4'];
            $scope.selectedpoint = $scope.point[1];
        });
    </script>
</head>
<body ng-app="mymodule">
    <div ng-controller="x">
        <select ng-model="selectedpoint" ng-options="item from items in point"></select>
        Select = {{selectedpoint}}
        <hr/>
        <div ng-switch="selectedpoint">
            <div style="border: 1px solid;" ng-switch-when="point1"> First file is selected</div>
            <div style="border: 1px solid;" ng-switch-when="point2"> Second file is selected</div>
            <div style="border: 1px solid;" ng-switch-when="point3"> Third file is selected</div>
            <div style="border: 1px solid;" ng-switch-when="point4"> Fourth file is selected</div>
        </div>
    </div>
</body>

正确的语法是:

 <select ng-model="selectedpoint" ng-options="item as item for item in point"></select>

对于:

<option value="point1">point1</option>
<option value="point2">point2</option>
<option value="point3">point3</option>

要么:

 <select ng-model="selectedpoint" ng-options="item for item in point"></select>

对于:

<option value="0">point1</option>
<option value="1">point2</option>
<option value="2">point3</option>

 <html> <head> <title>View</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script> <script> var mymodule = angular.module('mymodule' ,[]); mymodule.controller('x' , function($scope){ $scope.point = ['point1' , 'point2' , 'point3' , 'point4']; $scope.selectedpoint = $scope.point[2]; }); </script> </head> <body ng-app="mymodule"> <div ng-controller="x"> <select ng-model="selectedpoint" ng-options="item for item in point">1</select> Select = {{selectedpoint}} <hr/> <div ng-switch="selectedpoint"> <div style="border: 1px solid;" ng-switch-when="point1"> First file is selected</div> <div style="border: 1px solid;" ng-switch-when="point2"> Second file is selected</div> <div style="border: 1px solid;" ng-switch-when="point3"> Third file is selected</div> <div style="border: 1px solid;" ng-switch-when="point4"> Fourth file is selected</div> </div> </div> </body> 

玩得开心..!!

您在ng-options中犯了错误,我更正了您的代码

<select ng-model="selectedpoint" ng-options="item as item for items in point"></select>

点是控制器的作用域值。 分配给项目的代码,然后我们在项目之前溢出值和名称。

我已经修改了代码,您在Plunker中使用了“ from”而不是“ for” 正确的代码版本

<html>
    <head>
        <title>View</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>

        <script>
            var mymodule = angular.module('mymodule' ,[]);
            mymodule.controller('x' , function($scope){
                $scope.point = ['point1' , 'point2' , 'point3' , 'point4'];
                $scope.selectedpoint = $scope.point[1];
            });
        </script>
    </head>
    <body ng-app="mymodule">
        <div ng-controller="x">
            <select ng-model="selectedpoint" ng-options="item for item in point"></select>
            Select = {{selectedpoint}}
            <hr/>
            <div ng-switch="selectedpoint">
                <div style="border: 1px solid;" ng-switch-when="point1"> First file is selected</div>
                <div style="border: 1px solid;" ng-switch-when="point2"> Second file is selected</div>
                <div style="border: 1px solid;" ng-switch-when="point3"> Third file is selected</div>
                <div style="border: 1px solid;" ng-switch-when="point4"> Fourth file is selected</div>
            </div>
        </div>
    </body>
    </html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM