AngularJS - ng焦点和ng模糊错误

时间:2014-04-10 15:13:41

标签: javascript angularjs

我尝试为ngFocus和ngBlur添加代码,但每次启动它时,它似乎都不起作用,继承我的js代码:

function focusCtrl($scope) {
    $scope.hasFocus = false;
}
angular.module('app', [])
    .directive('ngFocus', ['$parse', function ($parse) {
        return function (scope, element, attr) {
            var fn = $parse(attr['ngFocus']);
            element.on('focus', function (event) {
                scope.$apply(function () {
                    fn(scope, {$event: event});
                });
            });
        };
    }])
    .directive('ngBlur', ['$parse', function($parse) {
        return function(scope, element, attr) {
            var fn = $parse(attr['ngBlur']);
            element.on('blur', function(event) {
                scope.$apply(function() {
                    fn(scope, {$event:event});
                });
            });
        };
    }]);

我也会添加我的HTML:

<div class="col-lg-10">
<input ng-blur="hasFocus=false" ng-focus="hasFocus=true" type="text"  required="required" class="form-control form-validation" id="name" placeholder="Name" ng-model="artist.name" />
</div>

每次重新加载页面时都会出现此错误,我似乎无法理解这个问题!

Uncaught TypeError: Object #<Object> has no method 'module' Focus.js:4
(anonymous function)

感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

那些指令带有角度(不确定它们是在

中引入的版本)

ngFocus

ngBlur

话虽如此,当你创建指令时,不要在它们前面添加ng。该前缀由angular保留。

"do not prefix your own directives with ng or they might conflict with directives included in a future version of Angular" reference

如果您希望通过控制器范围内的字段将焦点设置到控件的功能,请查看此question

答案 1 :(得分:1)

Brocco答案还可以, 如果您想制作焦点或模糊的自定义指令,可以查看example