angular.js - angularjs 路由页面指令绑定的问题
伊谢尔伦
伊谢尔伦 2017-05-15 16:56:45
[AngularJS讨论组]

用户访问首页的时候,会默认路由到注册页面register.htm,而register.htm中定义了属性指令ensure-unique,但是directive.js中指令没有绑定上,指令没有起作用,不知道是何原因,前端没有报错,请帮忙看下
首页index.htm



    

    
    
    登陆页面


service.js是空的,留着以后用

app.js

myApp = angular.module("myApp", ["ui.bootstrap", "ngRoute"]);

myApp.config(['$routeProvider',
    function ($routeProvider) {
        $routeProvider.
            when('/index', {
                templateUrl: 'templates/index.htm'
            }).
            when('/register', {
                templateUrl: 'templates/register.htm'
            }).
            when('/login', {
                templateUrl: 'templates/login.htm'
            }).
            otherwise({
                redirectTo: '/register'
            });
    }]);

register.htm 注册页面,这里增加了属性指令ensure-unique

directive.js

myAppDirective=angular.module("myAppDirective",[]);

myAppDirective.directive('ensureUnique', ['$http', function ($http) {
    return {
        require: 'ngModel',
        link: function (scope, ele, attrs, c) {
            console.log("executed!");
            ele.bind('blur', function () {
                if (attrs.ensureUnique == "username") {
                    $http({
                        method: 'POST',
                        url: 'check/' + attrs.ensureUnique + ".html",
                        data: {"username": scope.username}
                    }).success(function (data, status, headers, cfg) {
                        c.$setValidity('unique', true);
                    }).error(function (data, status, headers, cfg) {
                        c.$setValidity('unique', false);
                    });
                } else if (attrs.ensureUnique == "email") {
                    $http({
                        method: 'POST',
                        url: 'check/' + attrs.ensureUnique + ".html",
                        data: {"email": scope.email}
                    }).success(function (data, status, headers, cfg) {
                        c.$setValidity('unique', true);
                    }).error(function (data, status, headers, cfg) {
                        c.$setValidity('unique', false);
                    });
                }
            });
        }
    }
}]);
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(2)
淡淡烟草味

已经解决了,没有指令的module注入到myApp的module中

PHPz

楼主你好,你的问题是怎么解决的?为什么你还写了一个module呢?在app.js一个module不就可以了吗?

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号