Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.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_Angularjs Directive - Fatal编程技术网

Javascript 更新属性指令值

Javascript 更新属性指令值,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我是angular js的新手,我尝试了一些示例代码。我正在尝试一些东西,但它不起作用。下面的示例代码将向您介绍有关此的更多详细信息 Html代码 角JS代码 你可以看到一行,这是不工作的。当我尝试更改值数据警报时,这是属性指令,但它始终显示0。我也试着观察价值的变化,但效果不太好 Fiddle:问题在于,您依赖于在ng repeat呈现后添加警报属性,这意味着在$digest阶段之后。到那时,已经太晚了——您已经错过了修改DOM以便正确编译和链接它们的机会 解决此问题的方法是将ng repea

我是angular js的新手,我尝试了一些示例代码。我正在尝试一些东西,但它不起作用。下面的示例代码将向您介绍有关此的更多详细信息

Html代码

角JS代码

你可以看到一行,这是不工作的。当我尝试更改值数据警报时,这是属性指令,但它始终显示0。我也试着观察价值的变化,但效果不太好


Fiddle:

问题在于,您依赖于在ng repeat呈现后添加警报属性,这意味着在$digest阶段之后。到那时,已经太晚了——您已经错过了修改DOM以便正确编译和链接它们的机会

解决此问题的方法是将ng repeat放在父元素中,并声明性地添加“alert”属性:

    <div repeat-hello-world="{{repeat}}">
        <span ng-repeat="i in [1,2,3]">
        <button data-alert="{{i}}">{{i}}</button>
        </span>
    </div>

你能发布一个提琴吗?我已经添加了提琴链接并更新了我的问题。是的,你是对的,但是如果我们在呈现DOM后必须进行更新怎么办。这就是我正在寻找的。
'use strict';

angular.module('myApp.directives', [])
    .directive('repeatHelloWorld', ['$timeout', function (timer) {
    return {
        compile: function(element, attributes) {             
            var hello = function(){                 
                 var nodes = element.children();
                console.log(nodes.length);
                 for(var i=0; i<nodes.length; ++i){                 
                  console.log(i);    
                  angular.element(nodes[i]).attr('data-alert',i);   // This is not working
              }           
            }

            element.ready(function(){
                  hello();  
            });

            var nodes = element.children();
              for(var i=0; i<nodes.length; ++i){
                  console.log(nodes.length);
                  console.log(i);
                  angular.element(nodes[i]).attr('data-alert',i);
              }           
        }
    }
}]);
// Declare app level module which depends on filters, and services
var myApp = angular.module('myApp', ['myApp.directives']);

/* Controllers */
function testController($scope) {
    $scope.repeat = 5;
}

myApp.directive("alert", function(){     
    return function(scope, element, attrs){

        attrs.$observe(attrs.alert , function(newValue){
            console.log('newval' + newValue);
        });
        element.bind("click", function(){
            console.log(attrs.alert);           
        });
    };
});
    <div repeat-hello-world="{{repeat}}">
        <span ng-repeat="i in [1,2,3]">
        <button data-alert="{{i}}">{{i}}</button>
        </span>
    </div>