简体   繁体   English

指令中的指令-手表不起作用

[英]Directive in Directive - watch not working

Spend many hours, tried almost everything , but it doesn`t work .. 花了很多时间,几乎尝试了所有方法,但是没有用..

I have Angular directive witch call another angular directive .. I need to handle event in child directive. 我有Angular指令,女巫调用了另一个Angular指令..我需要在子指令中处理事件。 So my child directive look like : 所以我的孩子指令看起来像:

           restrict: 'E',
           replace: true,
           scope: {
               testResult: "=",

And in parent directive i have : 在父指令中,我有:

<my-search-directive test-result="newTest"></my-search-directive>

To handle changes in testResult , in parent directive i have this watch : 为了处理testResult的更改,在父指令中我需要监视:

  $scope.$watch('newTest', function (newVal, oldVal) {
      alert('test ' + newVal + ' ' + oldVal);
  }, true);

It fires only one time in the beginning and newVal and oldVal are undefined.. If i have service instead of 'master' directive it works (i`m passing parentScope and use it for watch) 它仅在开始时触发一次,并且newVal和oldVal是未定义的。如果我有服务而不是'master'指令,则它可以正常工作(我通过parentScope并将其用于监视)

In the beggining newTest was return complex object and i thing that could be the problem , but it`s not ,because it now work with simple string also. 在开始时,newTest是返回复杂对象,这可能是问题所在,但不是,因为它现在也可以使用简单的字符串。

Try to use watchcollection , but it not working also .. I need exactly this scenario , directive that use directive and i can figure it out how can it works ? 尝试使用watchcollection,但它也无法正常工作。我需要这种情况,使用use指令的指令,我可以弄清楚它如何工作?

EDIT 1 :

myApp.directive('parentDirective', [
    "$log", function ($log) {

        return {
            scope: {
                getQueriesUrl: "=",
                isFullyQualified: "=",
                isMainTabs: "=",
                model: "=",
                crudUrls: "=",
                callBackFunction: "&",
                callback: "&",
                newTestResult: "@"
            },
            restrict: 'A',
            replace: true,
            controller: function ($scope) 
{
myActions.push('<div><my-search-directive test-result="newTestResult"></my-search-directive></div>');
}

That is my 'parent directive' , so i need to track changes of test-result. 那是我的“父母指令”,所以我需要跟踪测试结果的变化。 As @Walfrat mantion , i added newTestResult variable , but still doesn`t work .. 作为@Walfrat指令,我添加了newTestResult变量,但仍然无法正常工作..

First of all you re not using directive in directive. 首先,您不要在指令中使用指令。

You're using a directive with 2 way binding here. 您在此处使用带有2种方式绑定的指令。

Since your scope is binded to data you should watch testResult as defined in your scope{} block. 由于您的范围已绑定到数据,因此您应该注意一下scope{}块中定义的testResult If $watch not working try watchCollection on it and you should be fine. 如果$ watch无法正常工作,请尝试使用watchCollection,这样就可以了。

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

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