繁体   English   中英

在AngularJS中使用不同控制器的指令?

[英]Use directive with different controllers in AngularJS?

我已经为搜索框创建了一个指令,我希望将其用于不同的视图。 这是指令 -

angular.module('jobSeekerApp')
  .directive('searchBoxDirective', function () {
    return {
      restrict: 'E',
      templateUrl: 'templates/searchbox-template.html',
    };
  });

指令的模板 -

<span class="searchButton"><i class="fa fa-search fa-2x"></i></span>
<input ng-change="search()" ng-model="searchTerm" ng-keydown="deleteTerm($event)" type="text" id="search-box" style="width: 0px; visibility:hidden;"/>

我想在两个看起来像这样的视图上使用这个指令 -

查看1 -

<div class="panel panel-default companies" ng-repeat="company in companies.companiesList">
    <div class="panel-heading text-center"><a ng-href="/companies/{{company.id}}" class="hvr-sink"><h3 class="well">{{company.company_name}}</h3></a></div>
    <div class="panel-body text-center flexcontainer">
        <div>Location: {{company.location}}</div>
        <div>Founded In: {{company.founded_year}}</div>
        <div ng-if="company.opening">Opening: Yes</div>
        <div ng-if="!company.opening">Opening: No</div>
        <div>Number Of Openings: {{company.no_openings}}</div>
    </div>
</div>

查看2 -

<div class="panel panel-default jobs" ng-repeat="job in jobs.jobsList">
    <div class="panel-heading text-center"><a href="{{job.career_url}}" target='_blank' class="hvr-sink"><h3 class="well">{{job.job_name}}</h3></a></div>
    <div class="panel-body text-center flexcontainer">
        <div>Company: {{job.company_name}}</div>
    </div>
</div>

正如您所看到的,我在视图中使用了别名companiesjobs ,因此我的指令无法影响它所包含的视图。如果我在模板中使用companiesjobs ,那么它可以正常工作。 例如,如果将模板更改为 -

<span class="searchButton"><i class="fa fa-search fa-2x"></i></span>
<input ng-change="companies.search()" ng-model="companies.searchTerm" ng-keydown="companies.deleteTerm($event)" type="text" id="search-box" style="width: 0px; visibility:hidden;"/>

然后它适用于与companies控制器相关联的视图,同样适用于jobs

如何将指令与相应的控制器实例一起使用? 谢谢。

为搜索创建一个简单的视图

创建自己的控制器

由该控制器在服务中搜索记录并将数据放入服务变量中

this.searchResult = [];
this.search = function(searchText){
    // code to search
    this.searchResult = Result;    
}

现在,您想要使用此结果,请在当前控制器中使用此服务变量上的监视,例如:

$scope.$watch(function() { return servicename.searchResult ; }, function(newVal, oldval) { 
   if(newval != oldval){
       $scope.data = servicename.searchResult;
       /* Do the rest of stuff */
   }
}, true);

使用隔离范围并明确将搜索项传递给您的指令。 就像是:

angular.module('jobSeekerApp')
  .directive('searchBoxDirective', function () {
    return {
      restrict: 'E',
      scope: {
        searchTerm: '=' <-- you can use '@' if you don't need two-way binding
      },
      templateUrl: 'templates/searchbox-template.html',
    };
  });

您没有显示实际使用指令的位置,但是您将通过属性传递scope属性(这是在<div>上使用您的指令):

<div search-box-directive search-term="companies.searchTerm"></div>

由于搜索功能是异步的,我建议避免使用ng-change来调用它。 而是使用ng-click搜索图标。

<span class="searchButton" ng-click="$ctrl.searchFn()">
    <i class="fa fa-search fa-2x"></i>
</span>
<input ng-model="$ctrl.searchTerm" type="text" id="search-box">

在该指令中,使用isolate scope和bindToController

app.directive('searchBoxDirective', function () {
    return {
      restrict: 'E',
      scope: { 'searchTerm': "=",
               'searchFn': "&"
             },
      controller: function () {},
      controllerAs: '$ctrl',
      bindToController: true,
      templateUrl: 'templates/searchbox-template.html'
    };
});

用法

<search-box-directive search-term="companies.searchTerm"
                      search-fn="companies.search()" >
</search-box-directive>

<search-box-directive search-term="jobs.searchTerm"
                      search-fn="jobs.search()" >
</search-box-directive>

search-term属性创建从父作用域到指令isolate作用域的双向绑定。 search-fn属性从父作用域创建表达式绑定以隔离作用域。

暂无
暂无

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

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