简体   繁体   English

AngularJS选择与ng-options调用函数吗?

[英]Angularjs select with ng-options calling a function?

here is my html 这是我的html

 <select ng-model="selectedMarker" ng-options="shape.text for shape in Selects('shapes')">
            </select>

and the code 和代码

 angular.module('todo', ['ionic'])
        .factory('Projects', function ($http) {
            return {
                options: function (type) {
            var selects = [{
                type: "shapes",
                list: [{
                    text: "Circle",
                    value: "S"
                }, {
                    text: "Polygon",
                    value: "P"
                }]
            }];
            var _filter = selects.filter(function (item) {
                if (item.type === type) {
                    return item.list;
                }
            });
            return _filter;
        },
     })
        .controller('TodoCtrl', function ($scope, $timeout, $ionicModal, Projects, $ionicSideMenuDelegate) {
            $scope.map = null;
            $scope.Selects = function (type) {
                var x= Projects.options(type);
                return x;
            }
    });

This results in rendering an empty select. 这导致呈现一个空的选择。 i replaced $scope.Selects as an array instead of calling a function, but then the select gets populated. 我将$ scope.Selects替换为数组而不是调用函数,但是随后填充了select。 Why is that ng-options does not work with a function? 为什么ng-options无法与函数一起使用? Is there anything wrong with my select tag? 我的选择标签有什么问题吗?

Your implementation is wrong (not to mention overly complicated). 您的实现是错误的(更不用说过于复杂了)。

The options function returns the original selects array and not the shapes array. options函数返回原始的selects数组,而不是shapes数组。
It is not clear what exactly are you trying to achive, but if you change var xx = _filter[0]; 目前尚不清楚您到底想达到什么目的,但是如果您更改var xx = _filter[0]; to var xx = _filter[0][type]; var xx = _filter[0][type]; it should work. 它应该工作。

I strongly suggest you take a closer look at what the functions you are using do (eg filter ), because there might be surprises coming your way... 我强烈建议您仔细查看正在使用的功能(例如filter ),因为您可能会感到惊讶……

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

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