繁体   English   中英

用作控制器的表示法时,使用 ng-options 进行选择

[英]Using ng-options for select when using as notation for controller

我有一个选择,使用 ng-options 来定义当我在 ng-controller 指令中只使用控制器名称时可以正常工作的选项。 问题是,当我向 ng-controller 添加“作为选项”时,它不再起作用(没有选项)。

不起作用:

<div ng-controller="optionsCtrl as options">
    <select ng-model="options.oxygenSource" ng-options="item.name for item in options.oxygenSources"></select><br />
</div>

作品:

<div ng-controller="optionsCtrl">
    <select ng-model="oxygenSource" ng-options="item.name for item in oxygenSources"></select>
</div>

如果有帮助,这是我的控制器:

.controller('optionsCtrl', ['$scope', 'adminServ', function ($scope, adminServ) {
    // User selections
    $scope.oxygenSource = null;

    $scope.oxygenSources = adminServ.getOxygenSources();
}])

有什么想法吗? 谢谢,杰森

你应该改变的controller ,当您使用controllerAs语法。 控制器应该使用this关键字而不是$scope

控制器

.controller('optionsCtrl', ['$scope', 'adminServ', function ($scope, adminServ) {
    var options = this;
    options.oxygenSource = null;
    options.oxygenSources = adminServ.getOxygenSources();
}])

控制器作为托德座右铭的信息文章

暂无
暂无

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

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