简体   繁体   English

如何冒泡将指令范围更改为其父角度的效果

[英]how to bubble up the effect of changing the scope of a directive to its parents angular

This is coming from a total angular.js noob. 这是来自总的angular.js新手。

basically this is what this prototype site looks like: 基本上,这是该原型网站的外观:

在此处输入图片说明

the idea is that the nav search bar is should be hidden upon initial page load.. and only the home search bar should show. 想法是,在初始页面加载时,导航搜索栏应被隐藏..而仅显示主页搜索栏。 as soon as the user enters something in the home search bar.. it should load the results on the map, make the home search bar disappear and the nav search bar appear 用户在首页搜索栏中输入内容后,就会将搜索结果加载到地图上,使首页搜索栏消失,同时出现导航搜索栏

i read a lot about angular directives.. and how directives can adopt its parent scopes etc, and how you can make isolate scopes and pick and choose which of the parent scopes you like to inherit 我读了很多有关角度指令的文章。以及指令如何采用其父作用域等,以及如何使隔离作用域以及如何选择要继承的父作用域

so this is what my jade/html looks like: 这就是我的jade / html的样子:

body(ng-controller='AppCtrl')
  div(ng-view)

  .container
    .top_nav_bar
      a.logo
      a.menu_button
    secondary_nav(ng-show="showNav", show-nav="showNav")

    div(ng-controller="ListingsCtrl")
      home_search(ng-controller="SearchCtrl",ng-show="showHomeSearch", show-home-search="showHomeSearch", show-nav="showNav")

inside controllers : 内部控制器

.controller('AppCtrl', 
  ..
  $scope.showNav = true

.controller('ListingsCtrl'
  ..
  showHomeSearch: true

inside directives: 内部指令:

.directive('secondaryNav',()->
  restrict: 'AEC'
  templateUrl: '/partials/secondary_nav.html'
  scope:
    showNav: '='
)

.directive('homeSearch', ->
    scope:
      map: '='
      showHomeSearch: '='
      showNav: '='
    restrict: 'AEC'
    replace: false
    templateUrl: '/partials/home_search.html'
    link: (scope, elem, attrs) ->
      search_button = elem.find('.button')
      search_button.bind "click", ->
        scope.$apply ()->
          scope.showHomeSearch = false
          scope.showNav = true
])

basically home search inherits the value of showHomeSearch from its parent controller/scope (and it's set to true).. so when i click on the search button.. its value turns to false and the home search indeed disappears. 基本上, home search从其父控制器/范围继承showHomeSearch的值(并将其设置为true)。因此,当我单击搜索按钮时。其值变为false,首页搜索实际上消失了。

as for the nav search bar .. if i set $scope.showNav to false in the AppCtrl , it indeed disappears. 至于nav search bar ..如果我在AppCtrl中将$scope.showNav设置$scope.showNav false ,它的确消失了。 but then clicking on the search button doesnt make it appear. 但随后单击搜索按钮并没有显示它。 Even though if I put a breakpoint on scope.showNav = true in the homeSearch directive, I can see that the value of scope.showNav is false and it changes to true.. but then nothing happens. 即使我在homeSearch指令中的scope.showNav = true上设置了一个断点,我也可以看到scope.showNav的值是false并且它更改为true。

any help? 有什么帮助吗? even if you show me a completely different way of doing the whole thing! 即使您向我展示了一种完全不同的整体方法!

I think it's because your controller $scope.showNav is a primitive and your running into the prototypal inheritance gotcha described here: https://github.com/angular/angular.js/wiki/Understanding-Scopes 我认为这是因为您的控制器$ scope.showNav是一个原始类型,并且您遇到了原型继承问题,描述如下: https : //github.com/angular/angular.js/wiki/Understanding-Scopes

Try using an object with a property of showNav instead. 尝试改用具有showNav属性的对象。

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

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