$ ionicNavBarDelegate.getTitle()不起作用

时间:2016-03-25 17:50:11

标签: angularjs angularjs-directive ionic-framework ionic-view

我正在使用离子框架,我有一个与自定义指令绑定的搜索图标。它就像Whatsapp的搜索图标一样,当你点击它时隐藏了视图标题并出现了搜索输入文本。

我的问题是视图标题未被隐藏。我正在使用:

<ion-nav-title>{{viewTitle}}</ion-nav-title>

两个更改我正在使用的视图标题:

title = $ionicNavBarDelegate.getTitle();
$ionicNavBarDelegate.setTitle('');

当我调试这一点时,我可以看到title变量未定义。任何人都可以帮我这个吗?

查看:

<ion-view>
  <ion-nav-title>{{viewTitle}}</ion-nav-title>
  <search-bar ng-model="search"></search-bar>
  <ion-content>
  </ion-content>
</ion-view>

搜索指令:

.directive('searchBar', [function () {
  return {
    scope: {
      ngModel: '='
},
require: ['^ionNavBar', '?ngModel'],
restrict: 'E',
replace: true,
template: '<ion-nav-buttons side="right">'+
        '<div class="searchBar">'+
          '<div class="searchTxt" ng-show="ngModel.show">'+
              '<div class="bgdiv"></div>'+
              '<div class="bgtxt">'+
                '<input type="text" placeholder="Procurar..." ng-model="ngModel.txt">'+
              '</div>'+
            '</div>'+
            '<i class="icon placeholder-icon" ng-click="ngModel.txt=\'\';ngModel.show=!ngModel.show"></i>'+
        '</div>'+
      '</ion-nav-buttons>',

compile: function (element, attrs) {
  var icon=attrs.icon
      || (ionic.Platform.isAndroid() && 'ion-android-search')
      || (ionic.Platform.isIOS()     && 'ion-ios7-search')
      || 'ion-search';
  angular.element(element[0].querySelector('.icon')).addClass(icon);

  return function($scope, $element, $attrs, ctrls) {
    var navBarCtrl = ctrls[0];
    $scope.navElement = $attrs.side === 'right' ? navBarCtrl.rightButtonsElement : navBarCtrl.leftButtonsElement;

  };
},
controller: ['$scope','$ionicNavBarDelegate', function($scope,$ionicNavBarDelegate){
  var title, definedClass;
  $scope.$watch('ngModel.show', function(showing, oldVal, scope) {
    if(showing!==oldVal) {
      if(showing) {
        if(!definedClass) {
          var numicons=$scope.navElement.children().length;
          angular.element($scope.navElement[0].querySelector('.searchBar')).addClass('numicons'+numicons);
        }

        title = $ionicNavBarDelegate.getTitle();
        $ionicNavBarDelegate.setTitle('');
      } else {
        $ionicNavBarDelegate.setTitle(title);
      }
    } else if (!title) {
      title = $ionicNavBarDelegate.getTitle();
    }
  });
    }]
  };
}])

0 个答案:

没有答案