Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在没有隔离作用域的指令中获得双向绑定_Javascript_Angularjs - Fatal编程技术网

Javascript 在没有隔离作用域的指令中获得双向绑定

Javascript 在没有隔离作用域的指令中获得双向绑定,javascript,angularjs,Javascript,Angularjs,我有这样的指示: angular.module('somemodule').directive('tooltipText', function ($timeout, $compile, $document, config, $filter) { return { restrict: 'A', scope:{ tooltip: '=' }, link: function (scope, element, attrs) { let t

我有这样的指示:

angular.module('somemodule').directive('tooltipText', function ($timeout, $compile, $document, config, $filter) {
  return {
    restrict: 'A',

    scope:{
       tooltip: '='
     },
    link: function (scope, element, attrs) {
      let tipHtml = `<div ng-class="tipClass">
                      <div class="header">
                        <span>{{ title }}</span>
                      </div>
                      <div class="body">
                        <br ng-show="showDescription && description"/>
                        <span ng-show="showDescription">{{description}}</span>
                      </div>
                      <div class="tooltip-arrow"></div> 
                    </div>`;

    scope.$watch('tooltip',(changedAttr) => {

    if (element) {
      let elm = angular.element(element)[0];
      if (elm.getAttribute('tooltip-show')) {

        if (changedAttr && elm.offsetParent != null) {
          showtooltip(element);
          elm.scrollIntoView();

        }
      } else {
        element.bind('mouseover', showtooltip);
      }
    }
  });
 showtooltip = (elem)=>{
 //do some tooltip logic and then after a timeout of say, 5secs, I do

   scope$.tooltip = false;
   //i.e i remove the tooltip and set the attribute
   // value back so that next time change can be 
   // watched by this directive. Basically it's a tooltip
   //that should last for around 5 seconds and when
   //setting the attribute on element again to true,
   // it should reappear.
   }
});
angular.module('somemodule')。指令('tooltipText',函数($timeout、$compile、$document、config、$filter){
返回{
限制:“A”,
范围:{
工具提示:'='
},
链接:函数(范围、元素、属性){
设tipHtml=`
{{title}}

{ if(元素){ 设elm=angular.element(element)[0]; if(elm.getAttribute('tooltip-show')){ if(changedAttr&&elm.offsetParent!=null){ 显示工具提示(元素); elm.scrollIntoView(); } }否则{ 元素绑定('mouseover',showtooltip); } } }); showtooltip=(元素)=>{ //执行一些工具提示逻辑,然后在超时5秒后执行 scope$.tooltip=false; //i、 e我删除工具提示并设置属性 //返回值,以便下次可以更改 //由这个指令监视。基本上它是一个工具提示 //这应该持续5秒左右 //将元素上的属性再次设置为true, //它应该重新出现。 } });

现在的问题是,使用此隔离作用域时,它与其他指令冲突。解决此问题的最佳方法是什么?

要避免使用隔离作用域,只需查看以下属性:

scope: false,  ̶{̶
          ̶t̶o̶o̶l̶t̶i̶p̶:̶ ̶'̶=̶'̶
      ̶}̶,̶
link: function (scope, element, attrs) {
    let tipHtml = `<div ng-class="tipClass">
                  <div class="header">
                    <span>{{ title }}</span>
                  </div>
                  <div class="body">
                    <br ng-show="showDescription && description"/>
                    <span ng-show="showDescription">{{description}}</span>
                  </div>
                  <div class="tooltip-arrow"></div> 
                </div>`;

    ̶s̶c̶o̶p̶e̶.̶$̶w̶a̶t̶c̶h̶(̶'̶t̶o̶o̶l̶t̶i̶p̶'̶,̶(̶c̶h̶a̶n̶g̶e̶d̶A̶t̶t̶r̶)̶ ̶=̶>̶ ̶{̶
    scope.$watch(attrs.tooltip,(changedAttr) => {
如果属性是可赋值的,则在范围上下文中将其设置为
false


请注意,双向绑定很难解释更改了哪些数据以及何时更改。只有拥有数据的组件才应该修改它。输入应该是单向的,输出应该作为回调实现。

这很好。但我需要将更改推回到父属性。这将如何工作?比如说10秒后,我将需要执行类似attrs.tooltip=false的操作;这不是像attrs.$set那样的静态值集。我需要实际绑定..否则在这10秒之后,该值仍然为true,并且工具提示不会在使用服务时触发。
function postLink(scope, elem, attrs) {
    var getter = $parse(attrs.tooltip);
    var setter = getter.assign;
    setter(scope,false);
};