微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – AngularJS – 在Object中获取值的指令

目前我正在使用这个for循环来获取父项
angular.forEach(queryTicketCategories,function(category) {
    if(category.id === $scope.ticketCategory.parentId) {
        $scope.parent = category;
    }
});

请建议将返回类别的常用指令.这里的queryTicketCategories是数组的对象.而且我想为$scope.parent指定一个数组,它等于$scope.ticketCategory.parentId

HTML代码

<input type="text" ng-model="parent" 
   placeholder="{{'PARENT_CATEGORY' | translate}}" 
   typeahead="category as category.name for category in getTicketCategories($viewValue)" 
   typeahead-loading="loading" class="form-control">

解决方法

对于您的情况,最好是查看typahead指令并最大限度地利用它.

http://angular-ui.github.io/bootstrap/#/typeahead

而不是创建自己的指令或服务,您可以使用带有回调的现有伪指令,而不用选择

typeahead-on-select($item,$model,$label) (Defaults: null) : 
A callback executed when a match is selected

这是我创建而不使用回调的示例.您可以从您输入的内容中选择一个Google地址.

没有类型的选择:
http://plnkr.co/edit/GMnzod9MQZWxsKSuJUJu?p=preview

通过将所选地址更改为大写,以下步骤进一步.你可以在这个回调中做任何你想要的.

http://plnkr.co/edit/jaeSZdSKucMQgIF05KwD?p=preview

我使用此功能将所选地址更改为大写.

$scope.myFunc = function($item,$label) {
    $scope.asyncSelected = $item.toupperCase();
}

对于你的情况,你可以做如下

$scope.myFunc = function(category) {
    if(category.id === $scope.ticketCategory.parentId) {
        $scope.parent = category;
    }
}

HTML

typeahead-on-select="myFunc($item)"

总而言之,您的用例和数据可能与上面使用的示例不同,但主要方法是相同的.选择一个项目后有一个回调,您可以使用选择的回调类型来更好地处理您的数据控制.

原文地址:https://www.jb51.cc/js/151846.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐