Advertisement
Guest User

Untitled

a guest
May 5th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. <parent>
  2. <label>Username: </label>
  3. <child></child>
  4. <button id="login" type="submit">Login</button>
  5. </parent>
  6.  
  7. angular.module('sampleModule', [])
  8. .directive("parent",function(signOnService){return {
  9. restrict : 'E',
  10. transclude: true,
  11. scope: {},
  12. controller: function($scope, $element, $attrs){
  13. this.username = 'InitU';
  14. this.password = 'InitP';
  15. this.setUsername = function(name){
  16. this.username = name;
  17. };
  18. this.getUsername = function(){
  19. return this.username;
  20. };
  21. this.getPassword = function(){
  22. return this.password
  23. };
  24. this.setPassword = function(password){
  25. this.password = password;
  26. };
  27. this.check = function() {
  28. //TODO something on form submission
  29. };
  30. },
  31. controllerAs: 'diff',
  32. link: function(scope, element, attrs, ctrl){
  33. scope.user = ctrl.username;
  34. element.find('button').on('click', function(event){
  35. alert('Username: '+ ctrl.username + 'nPassword: '+ ctrl.password);
  36. });
  37. },
  38. template: '<form name="myForm" ng-submit="diff.check()"><ng-transclude></ng-transclude></form>'
  39. }})
  40. .directive("child",function(){
  41. return {
  42. restrict : 'E',
  43. require: '^parent',
  44. scope: false,
  45. link: function(scope, element, attrs, ctrl){
  46. scope.username = ctrl.getUsername();
  47. console.log('INI: '+ scope.username);
  48.  
  49. scope.$watch('username', function(newVal, oldVal){
  50. if(newVal!=oldVal)
  51. //alert('Value Changed: '+ newVal);
  52. ctrl.setUsername(scope.username);
  53. });
  54. },
  55. template: '<input name="username" type="text" ng-model="username" required>'
  56. }
  57. });
  58.  
  59. <parent>
  60. <label>Username: </label>
  61. <child></child>
  62. <button id="login" type="submit" ng-disabled="myForm.$invalid">Login</button>
  63. </parent>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement