加载时输入指令事件

时间:2015-12-31 03:23:04

标签: javascript angularjs

我为input创建了指令。如果输入不为空,我需要更改类名:

return {
    restrict: "C",
    link: function(scope, elem, attr) {

        elem.find('input, textarea').bind('focus', function() {
            elem.find('label').addClass('active');
        });

        elem.find('input, textarea').bind('blur', function(self) {
            if (self.target.value == '') elem.find('label').removeClass('active');
        });

        elem.find('input, textarea').bind('change', function(self) {
            if (self.target.value != '') elem.find('label').addClass('active');
        });
    }
}

以下是我的意见:

<div class="input">
        <input type="text" name="name" ng-model="name" />
        <label for="name">Name</label>
    </div>

Focus and blur工作正常。但change没有。在加载value = ''时。但我的$scope.name不是空的。

如果ng-model在加载时不为空,我该如何更改班级名称?

更新

        setTimeout(function() {
            if (elem.find('input, textarea').val() != '') elem.find('label').addClass('active');
        }, 100);

有效。但我想也许在一些旧浏览器中应该是200ms或1000ms ......

1 个答案:

答案 0 :(得分:-1)

我认为您可以使用def isActionDictMultipleTargets(action_type,author_gae_key,target_gae_keys_list): out_dict = {} if(target_gae_keys_list): actions = ndb.gql("SELECT * FROM UserAction WHERE author_gae_key = :1 AND action_type = :2 AND target_gae_key IN :3",author_gae_key,action_type,target_gae_keys_list) for an_action in actions: out_dict[an_action.target_gae_key.urlsafe()] = True false_keys = list(set(target_gae_keys_list)-set(out_dict.keys())) for a_key in false_keys: out_dict[a_key] = False return out_dict 等待0秒,或$timeout(function () { ... })

您可以找到更多信息 http://www.bennadel.com/blog/2605-scope-evalasync-vs-timeout-in-angularjs.htm