ng-click不会触发,功能不起作用。可能是范围?

时间:2015-06-08 15:35:39

标签: javascript jquery html asp.net angularjs

我正在尝试让动画工作,当您单击搜索按钮时,它应该被拉到右侧,以便允许用户键入他们想要搜索的内容。但是,ng-click似乎甚至没有触发。我试过“警告('工作');”几次,但没有弹出。代码如下,任何关于问题可能是什么的想法?

HTML:

<li class="pull-right">
    <div>
    <!-- ngIf: showNavbarSearch --><div ng-if="showNavbarSearch" class="mat-slide-right pull-right ng-scope">
                                <form class="search-form form-inline ng-valid pull-left ng-pristine" ng-show="showNavbarSearch" ng-submit="submitNavbarSearch()">
                                    <div class="form-group">
                                        <label class="sr-only" for="search-input">Search</label>
                                        <input type="text" class="form-control" id="search-input" placeholder="Search" autofocus="">
                                    </div>
                                </form>
                            </div><!-- end ngIf: showNavbarSearch -->

                            <div class="pull-right">
                                <button ng-click="toggleSearch()" class="btn btn-sm btn-link pull-left withoutripple">
                                    <i class="md md-search f20"></i>
                                </button>
    </div>
</li>

JS:

app.directive('navbarSearch', ['$timeout', function ($timeout) {
  return {
    restrict: 'A',
    templateUrl: '/dist/assets/tpl/directives/navbar-search.html',
    link: function($scope, element, attrs) {
      $scope.showNavbarSearch = false;

      $scope.toggleSearch = function () {
        $scope.showNavbarSearch = !$scope.showNavbarSearch;
      };

      $scope.submitNavbarSearch = function(){
        $scope.showNavbarSearch = false;
      };
    }
  };
}]);

1 个答案:

答案 0 :(得分:0)

这有用吗:

    app.directive('navbarSearch', [ function () {
    return {
        restrict: 'A',
        templateUrl: 'navbar-search.html',
        controller: function($scope){
          $scope.showNavbarSearch = false;
          $scope.mySearch = "";
          $scope.submitNavbarSearch = function(myVal){
            $scope.mySearch = myVal;
            console.log("submitting: " + $scope.mySearch);
            $scope.showNavbarSearch = false;
          }

          $scope.toggleSearch = function(){
            $scope.showNavbarSearch = !$scope.showNavbarSearch;
          }
        },

    };
}]);

模板:

<li class="pull-right">
<div ng-if="showNavbarSearch" class="mat-slide-right pull-right ng-scope">
  <form class="search-form form-inline ng-valid pull-left ng-pristine" ng-show="showNavbarSearch" ng-submit="submitNavbarSearch(mySearch)">
      <div class="form-group">
          <label class="sr-only" for="search-input">Search</label>
          <input type="text" ng-model="mySearch" placeholder="Search" autofocus="">
      </div>
  </form>
</div><!-- end ngIf: showNavbarSearch -->
<br />
<div class="pull-right">
  <button ng-click="toggleSearch()">
      <i class="md md-search f20">Toggle search</i>
  </button>
</div>
</li>

用法:

<div navbar-search>

    </div>