Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.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 有时不调用AngularJS指令_Javascript_Html_Angularjs_Angularjs Directive - Fatal编程技术网

Javascript 有时不调用AngularJS指令

Javascript 有时不调用AngularJS指令,javascript,html,angularjs,angularjs-directive,Javascript,Html,Angularjs,Angularjs Directive,我的指示: ppm.directive('focusMe', function($timeout) { return { link: function(scope, element, attrs) { scope.$watch(attrs.focusMe, function(value) { if(value === true) { console.log('value=',value);

我的指示:

ppm.directive('focusMe', function($timeout) {
return {
    link: function(scope, element, attrs) {
        scope.$watch(attrs.focusMe, function(value) {
            if(value === true) { 
                console.log('value=',value);
                //$timeout(function() {
                element[0].focus();
                scope[attrs.focusMe] = false;
                //});
            }
        });
    }
};
});
有时调用指令,有时不调用。它可以位于同一个HTML文件中,其中某些元素可以具有标记,对于某些元素,该指令被调用,而对于其他元素,该指令不被调用

focus-me="true" 
我甚至有两个具有相同代码的不同HTML文件,但其中一个从未调用过该指令。例如,此代码可以在一个HTML文件中工作,而不能在另一个HTML文件中工作

<div class="row form-group">
<div class="col-xs-12" style="text-align:center;">
    <button class="btn btn-default" ng-click="addMore();" tabindex="5" focus-me="true">
        <span class="glyphicon glyphicon-plus button-add"></span>
        Add more
    </button>
</div>
</div>

添加更多

这可能是什么原因造成的?控制器是否对指令执行任何操作?在一个变量上添加一个
的$watch
会让人觉得很奇怪,这个变量不会改变,除非从指令内部改变它。您是否曾经将true传递给过“聚焦我”?如果没有,您可以不用手表,只需使用以下各项:

ppm.directive('focusMe',函数($timeout){
返回{
链接:函数(范围、元素、属性){
元素focus();
}
};
});
一些文本

您在一个变量上添加了一个
$watch
,该变量不会改变,除非您从指令内部改变它。您是否曾经将true传递给过“聚焦我”?如果没有,您可以不用手表,只需使用以下各项:

ppm.directive('focusMe',函数($timeout){
返回{
链接:函数(范围、元素、属性){
元素focus();
}
};
});
一些文本
好的,我解决了我的问题。 没有调用指令的元素实际上被调用了,但在错误的时间/视图中。因此,每当加载视图(元素所在的位置)时,都不会调用focus me指令,因为它已在以前的视图中被调用。本质上,我必须将ng show更改为ng if

这:


为此:

<div class="container-fluid">
<div dn-breadcrumbs dn-value="{{data.unitDn}}" ignore-levels="2" ng-if="currentStep > 0"></div>
<div ng-include="'templates/report/step1.html'" ng-controller="ReportStep1Controller" ng-show="currentStep == 0"></div>
<div ng-include="'templates/report/vri-step2.html'" ng-if="currentStep == 1"></div>
<div ng-include="'templates/report/vri-step3.html'" ng-if="currentStep == 2"></div>

好的,我解决了我的问题。 没有调用指令的元素实际上被调用了,但在错误的时间/视图中。因此,每当加载视图(元素所在的位置)时,都不会调用focus me指令,因为它已在以前的视图中被调用。本质上,我必须将ng show更改为ng if

这:


为此:

<div class="container-fluid">
<div dn-breadcrumbs dn-value="{{data.unitDn}}" ignore-levels="2" ng-if="currentStep > 0"></div>
<div ng-include="'templates/report/step1.html'" ng-controller="ReportStep1Controller" ng-show="currentStep == 0"></div>
<div ng-include="'templates/report/vri-step2.html'" ng-if="currentStep == 1"></div>
<div ng-include="'templates/report/vri-step3.html'" ng-if="currentStep == 2"></div>


>似乎需要$timeout来提供渲染的模式时间。这并没有解决我的问题。>>$timeout似乎需要提供模式时间来渲染。这并没有解决我的问题。这并没有解决我的问题。很奇怪的是,focus-me标签在某个元素上工作,而对某些元素却没有。这并没有解决我的问题。奇怪的是,focus-me标记正在处理某些元素,而对于某些元素则没有。