【问题标题】:changes in directive scope variables don't update the parent scope variables. Angular Js 1.5指令范围变量的更改不会更新父范围变量。角度 Js 1.5
【发布时间】:2017-09-28 06:44:33
【问题描述】:

这是我的控制器

 function commentController($stateParams, commentFactory, $rootScope){
  const ctrl = this;
  ctrl.comment = {};
  ctrl.decision = {};
  ctrl.$onInit = function(){
    ctrl.decision.replyVisible = false;
  }

export {commentController}

这是我的组件

import {commentController} from './comment.controller'

export const commentComponent = 'commentComponent'
export const commentComponentOptions = {
  bindings: {
    post: '<'
  },
  templateUrl: 'blog/src/app/components/post/comment/comment.html',
  controller: ['$stateParams', 'commentFactory', '$rootScope', commentController],
  controllerAs: 'cm'
}

这是我的指令

 function showReply(){
  return {
    restrict: 'A',
    scope: {
      collapsed: '<'
    },
    link: function(scope, element, attrs){
      element.on('click', ()=>{
        console.log(`Trying to hide ${scope.collapsed}`);
        scope.collapsed = true
        console.log(`Trying to hide ${scope.collapsed}`);
      })
    }
  }
}

function hideReply(){
  return {
    restrict: 'A',
    scope: {
      collapsed: '<'
    },
    link: function(scope, element, attrs){
      element.on('click', ()=>{
        scope.collapsed = false;
        console.log(`Trying to hide scope.replyVisible is ${scope.collapsed}`);
      })
    }
  }
}

export {showReply, hideReply}

这是我的html

 <div class="comment_wrapper">
  <div class="comment" ng-repeat="comment in cm.post.comments">
    <h3>{{comment.author.name}}</h3>
    <a href="" ng-hre="mailto:{{comment.author.email}}"></a>
    <p ng-bind-html="comment.comment"></p>
    <button show-reply collapsed="cm.decision.replyVisible">Reply</button>
    <div class="reply_wrapper" ng-show="cm.decision.replyVisible">
      <label for="name">Name</label>
      <input type="text" id="name" ng-model="cm.comment.author.name">
      <label for="email">Email</label>
      <input type="text" id="email" ng-model="cm.comment.author.email">
      <label for="comment">Comment</label>
      <textarea name="" id="comment" cols="10" rows="5" ng-model="cm.comment.comment"></textarea>
      <button ng-click="cm.reply(comment._id)">Submit</button> <button hide-reply collapsed="cm.decision.replyVisible">Cancel</button>
    </div>
  </div>
</div>

这是我的模块

import angular from 'angular'
import {showReply, hideReply} from './comment.directive'
import {commentFactory, commentFactoryFunc} from './comment.factory'
import {commentComponent, commentComponentOptions} from './comment.component'

export const comment = angular.module('comment', [uiRouter])
.factory(commentFactory, ['$http', commentFactoryFunc])
.component(commentComponent, commentComponentOptions)
.directive('showReply', showReply)
.directive('hideReply', hideReply)
.name

这就是我想要做的,在我的 html 中,我想在有人点击回复按钮时显示回复表单。而且由于有多个 cmet,每个都有一个回复表单,我不能使用常规的 ng-click

scope.collapsed 所做的更改不会反映在cm.decision.replyVisible

我该如何进行这项工作?我一直对从指令访问父控制器变量感到困惑。

【问题讨论】:

  • 可能是因为你使用了单向绑定post: '&lt;'尝试post: '='
  • 这不是关于post 我试图隐藏回复表单。而且,我试图避免双向数据绑定,正如很多人在引入 Angular 1.5 时提到的那样
  • 是的,对不起,它是关于collapsed: '=',如果你想从孩子改变父母的东西,你需要使用双向绑定。
  • 但是我对每条评论都有一个回复表。如果我通过双向数据绑定绑定collapsed,那么它也会反映其他回复表单上的更改。
  • 是的,你需要在comment.decision.replyVisible而不是cm中保持状态collapsed

标签: angularjs angularjs-directive angularjs-components


【解决方案1】:

您应该使用comment.decision.replyVisible 而不是cm.decision.replyVisible

HTML

 <div class="comment_wrapper">
  <div class="comment" ng-repeat="comment in cm.post.comments">
    <h3>{{comment.author.name}}</h3>
    <a href="" ng-hre="mailto:{{comment.author.email}}"></a>
    <p ng-bind-html="comment.comment"></p>
   <button show-reply 
     collapsed="comment.decision.replyVisible">Reply</button>
   <div class="reply_wrapper" ng-show="comment.decision.replyVisible">
     <label for="name">Name</label>
     <input type="text" id="name" ng-model="cm.comment.author.name">
     <label for="email">Email</label>
     <input type="text" id="email" ng-model="cm.comment.author.email">
     <label for="comment">Comment</label>
     <textarea name="" id="comment" cols="10" rows="5" ng-model="cm.comment.comment"></textarea>
     <button ng-click="cm.reply(comment._id)">Submit</button> <button hide-reply collapsed="cm.decision.replyVisible">Cancel</button>
  </div>
 </div>
</div>

【讨论】:

  • 嘿,这不起作用。老实说,我不想让回复表单依赖于 ng-repeat 元素,因为数据来自后端。我什至在collapsed 变量上尝试了两种方式的数据绑定。这不起作用,然后我将它与scope.$apply 结合起来,但这反映了每个回复表单的变化
  • 嘿,所以我终于继续使用您的解决方案。虽然,我必须遍历从服务器接收到的 post 对象中的 cmets 数组,并创建一个新属性 replyVisible 并将其设置为 false,这并不让我感觉良好。但感谢您的帮助。我会保留这个解决方案,直到找到更好的解决方案。
猜你喜欢
  • 2015-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-21
  • 1970-01-01
  • 2014-05-18
  • 2017-05-10
  • 2015-12-10
相关资源
最近更新 更多