Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了angularjs – 使用Angular UI Directive Typeahead重定向到链接大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在为项目使用Angular UI Bootstrap typeahead指令,我想根据typeahead中选择的内容重定向到动态URl.我正在尝试将typeahead用作搜索框.

我查看了文档(尝试RTFM),所以我知道有一个我可以使用的typeaheadOnSELEct属性,但我不知道如何将其与@L_772_5@联系起来.我正在使用对象的JSON文件,每个对象都有一个特定的ID.我希望能够直接在typeahead属性中@L_772_5@,如下所示:

<div class='container-fluid' ng-controller="TypeaheadCtrl">
        <input type="text" id="search" ng-model="SELEctedPerson" typeahead="person as person.name for person in person | filter:$viewValue" typeahead-min-length="3" typeaheadOnSELEct="#/100_Ages/{{person.iD}}" ng-init="" />
    </div>

但那没用.我想我需要一个特定的控制器,但我不确定. typeahead指令似乎工作得很好,所以我想有一个简单的解决方案,但我找不到它.

现在,我的Typeahead控制器看起来像这样

function TypeaheadCtrl($scope,$http) {
  $http.get('person.json').success(function(data) {
      $scope.person = data;
      });
}

This plunkr显示了一切都在行动.我的路由器是使用基于每个JSON id的动态URL设置的,如下所示:

angular.module('app',['ui.bootstrap']).
    config(['$routeProvider',function($routeProvider) {
    $routeProvider.
        when('/app/:personId',{templateUrl: 'partials/person.html',controller: DetailCtrl}).
    }]);

解决方法

您可以观察对SELEctedPerson模型的更改.这是一个例子:

function TypeaheadCtrl ($scope,$http,$LOCATIOn,$log,$timeout) {
  $http.get('person.json').success(function(data) {
      $scope.person = data;
  });
  $scope.$watch('SELEctedPerson',function(newValue,oldvalue) {
    if (newvalue){
      $log.info('/100_Ages/' + $scope.SELEctedPerson.id);
      $LOCATIOn.path('/100_Ages/' + $scope.SELEctedPerson.id);
    }
  });
}

更新:更新到v0.4.0后,我能够这样做:

function TypeaheadCtrl ($scope,$timeout) {
  $http.get('person.json').success(function(data) {
      $scope.people = data;
  });
  $scope.onSELEct = function($item,$model,$label){
    $scope.$item = $item;
    $scope.$model = $model;
    $scope.$label = $label;
    $log.info($scope.$item);
    $log.info($scope.$model);
    $log.info($scope.$label);
    $LOCATIOn.path('/person/' + $scope.$item.id);
  }
}

然后在HTML中,我这样做了

<input type="text" id="search" placeholder="Search for a name or age" ng-model="SELEctedPerson" typeahead="person.id as person.name for person in people | filter:$viewValue" typeahead-on-SELEct="onSELEct($item,$label)" typeahead-min-length="3" ng-init="" />

大佬总结

以上是大佬教程为你收集整理的angularjs – 使用Angular UI Directive Typeahead重定向到链接全部内容,希望文章能够帮你解决angularjs – 使用Angular UI Directive Typeahead重定向到链接所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。