i defined blew directive:
define(['angular', './routes', './services', './controllers'], function(angular, userroutes, userservice, usercontroller) { 'use strict'; var app = angular.module('yourprefix.user', ['ngcookies', 'ngroute', 'user.routes', 'user.services']); app.directive('pwcheck', function ($document) { return { restrict: 'a', require: 'ngmodel', link: function (scope, elem, attrs, ctrl) { var firstpassword = '#' + attrs.pwcheck; elem.add(firstpassword).on('blur', function () { scope.$apply(function () { var v = elem.val()===$(firstpassword).val(); ctrl.$setvalidity('pwmatch', v); }); }); } }; }).directive('oldpwdcheck', function($document, $log, userservice){ return { restrict: 'a', require: 'ngmodel', link: function(scope, elem, attrs, ctrl) { var oldpwd = '#' + attrs.oldpwdcheck; elem.add(oldpwd).on('blur', function() { scope.$apply(function(){ var pass = {}; pass.value = elem.val(); userservice.validatepassword(pass).then(function(result) { $log.info(result); ctrl.$setvalidity('oldpwdmatch', result); }); }); }); } }; }); return app; });
for dependency userservice definition:
define(['angular', 'common'], function (angular) { 'use strict'; var mod = angular.module('user.services', ['yourprefix.common', 'ngcookies']); mod.factory('userservice', ['$http', '$q', 'playroutes', '$cookies', '$log', function ($http, $q, playroutes, $cookies, $log) { var user, token = $cookies['xsrf-token']; /* if token assigned, check token still valid on server */ if (token) { $log.info('restoring user cookie...'); playroutes.controllers.users.authuser().get() .success(function (data) { $log.info('welcome back, ' + data.name); user = data; }) .error(function () { $log.info('token no longer valid, please log in.'); token = undefined; delete $cookies['xsrf-token']; return $q.reject("token invalid"); }); } return { loginuser: function (credentials) { return playroutes.controllers.application.login().post(credentials).then(function (response) { // return promise can chain token = response.data.token; return playroutes.controllers.users.authuser().get(); }).then(function (response) { user = response.data; return user; }); }, logout: function () { // logout on server in real app delete $cookies['xsrf-token']; token = undefined; user = undefined; return playroutes.controllers.application.logout().post().then(function () { $log.info("good bye "); }); }, getuser: function () { return user; }, modifypassword: function(newpwd) { return playroutes.controllers.users.modifypassword(user.id).put(newpwd).then(function(response) { return response.data; }); }, validatepassword: function(oldpwd) { return playroutes.controllers.users.validatepassword(user.id).put(oldpwd).then(function(response) { return response.data; }).catch(function(error){ $log.error(error); return false; }); } }; }]); /** * add object route definition allow resolving route if user * logged in. adds contents of objects dependency of controller. */ mod.constant('userresolve', { user: ['$q', 'userservice', function ($q, userservice) { var deferred = $q.defer(); var user = userservice.getuser(); if (user) { deferred.resolve(user); } else { deferred.reject(); } return deferred.promise; }] }); /** * if current route not resolve, go start page. */ var handlerouteerror = function ($rootscope, $location) { $rootscope.$on('$routechangeerror', function (/*e, next, current*/) { $location.path('/'); }); }; handlerouteerror.$inject = ['$rootscope', '$location']; mod.run(handlerouteerror); return mod; });
the code works fine on local machine, error raised when deployed code remote aliyun machine. learning angular , not able figure out what's problem. how can fix it?
error: [$injector:unpr] http://errors.angularjs.org/1.3.15/$injector/unpr?p0=eprovider%20%3c-%20e%20%3c-%20oldpwdcheckdirective
implicit annotation
careful: if plan minify code, service names renamed , break app.
you need ensure code using inline array annotation of di while injecting dependency. if not using cause issue while minifying javascript.
app.directive('pwcheck',['$document', function ($document) { return { restrict: 'a', require: 'ngmodel', link: function (scope, elem, attrs, ctrl) { //code here } }; }]).directive('oldpwdcheck', ['$document', '$log', 'userservice', function($document, $log, userservice){ return { restrict: 'a', require: 'ngmodel', link: function(scope, elem, attrs, ctrl) { //..code..herer.. } }; }]);
Comments
Post a Comment