Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.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 如何使子指令“$watch”成为父范围?_Javascript_Angularjs - Fatal编程技术网

Javascript 如何使子指令“$watch”成为父范围?

Javascript 如何使子指令“$watch”成为父范围?,javascript,angularjs,Javascript,Angularjs,我试图让一个child指令监听父指令范围上的更改;然而,$watch并没有起作用。我觉得我错过了一些非常简单的事情 该代码的前提是侦听select更改并从对象中获取属性,然后将其传递给子指令。我正在使用scope.$parent.$watch,但这不起作用。有什么想法吗 /* ** method - fnDirective ** desc - this is the main directive for the select/un select all */ function fnDirect

我试图让一个child指令监听父指令范围上的更改;然而,$watch并没有起作用。我觉得我错过了一些非常简单的事情

该代码的前提是侦听select更改并从对象中获取属性,然后将其传递给子指令。我正在使用scope.$parent.$watch,但这不起作用。有什么想法吗

/*
** method - fnDirective 
** desc - this is the main directive for the select/un select all
*/
function fnDirective(){
    return {
        restrict : "AE",
        replace : true,
        template : '\
            <div class="modal-form-wrapper">\
                <div class="form-control">\
                    <label for="attribute-filter">Selection Set Attribute</label>\
                    <select id="attribute-filter" name="attribute-filter">\
                        <option value="0">None</option>\
                        <option ng-repeat="option in options" value="{{ option.id }}" ng-selected="option.is_selected">{{ option.name }}</option>\
                    </select>\
                </div>\
                <list-attribute-options selections="selections"></list-attribute-options>\
            </div>\
        ',
        scope : {
            options : "=",
            selections : "="
        },
        link : function( scope, elem, attrs ){

            //on select change get the attribute selections
            elem.find( "select" ).on( "change", function( vent ){
                var select, option, attribute_id, selections;                   
                select = angular.element( this );
                option = select.find( "option:selected" );      
                attribute_id = parseInt( option.val() );    
                //get the selections off array      
                scope.selections = _.first( 
                    _.where( scope.options, { id : attribute_id }) 
                ).selections;
            });

        }

    };
}
/*
** method - fnListAttributeOptions 
** desc - list out the selections based on the selected attribute
*/
function fnListAttributeOptions(){
    return {
        restrict : "AE",
        replace : true,
        template : '\
            <div>\
                <ul class="list-form-options">\
                    <li ng-repeat="selection in selections">\
                    </li>\
                </ul>\
            </div>\
        ',
        scope : {
            selections : "="
        },
        link :  function( scope, elem, attrs ){
            scope.$parent.$watch( "selections", function( new_value, old_value ){
                cl( new_value, old_value );
            });
        }

    }
}   

我把这件事弄明白了。需要运行作用域。使用数据设置变量后,$apply。作用域未侦听的原因是我在jQuery函数中设置作用域

            link : function( scope, elem, attrs ){
            //on select change get the attribute selections
            elem.find( "select" ).on( "change", function( vent ){
                var select, option, attribute_id, selections;                   
                select = angular.element( this );
                option = select.find( "option:selected" );      
                attribute_id = parseInt( option.val() );    
                //get the selections off array      
                scope.selections = _.first( 
                    _.where( scope.options, { id : attribute_id }) 
                ).selections;
                //!!APPLY!!!!
                scope.$apply();
            });
        }

我可以让您试试:$scope.$parent.$parent.$watch'property',functionvalue{/*…*/}。。。。您也可以尝试广播一个事件。如果选择是一个需要深入观察的对象数组,请添加true作为第三个参数