1

I have a email verification link, which looks something like

 .when('/user/register/verify/:verify_code?', {
      controller: 'AngularRegisterVerifyPage',
      templateUrl: '/user/register/verify'
 })

And my input field is like this.

input(ng-model="formData.verify_code", name="verify_code", type='text', class='form-control', placeholder='Verification Code', id="verify_code")

And this code should run when the template loads to insert the Parameter into the text field

 document.getElementById('verify_code').value = "Test"; // Debugging Test Code
 $scope.formSubmitted = false;
 if ($routeParams.hasOwnProperty("verify_code")) {
      document.getElementById('verify_code').value = $routeParams.verify_code;
 }

But it would appear that the controller executes this before the template actually loads because when the page loads the input field is empty. Any information on how to do this properly would be great, I tried googling things like "Load Template after Controller loads" but haven't found anything. Thanks.

Edit:

I tried adding ng-init="loadCode()" to the input field, but it still doesn't work.

 $scope.loadCode = function() {
      if ($routeParams.hasOwnProperty("verify_code")) {
           console.log('Called');
           document.getElementById('verify_code').value = $routeParams.verify_code;
           console.log($routeParams.verify_code);
           console.log(document.getElementById('verify_code').value);
      }
 }
5
  • Why are you using document.getElementById instead of just retrieving the value the Angular way as $scope.formdata.verify_code? You should only set the value by changing the model as well. Commented Nov 5, 2014 at 3:02
  • @Blazemonger Like I said, I'm new to Angular I wasn't aware I could set the value like that. Regardless it still doesn't work. Commented Nov 5, 2014 at 3:03
  • USTED Dębe manipulate DOM Solo en directivas Commented Nov 5, 2014 at 3:04
  • Put together a working example at plnkr.co if you can. I strongly suspect the real problem is that you're trying to manipulate the DOM instead of the model like Angular expects you to. Commented Nov 5, 2014 at 3:04
  • @Chandermani If he use ng-route to change ng-view template&controller. There is not have ng-controller="AngularRegisterVerifyPage". And according to his description, controller is working fine. So just need to change $scope.formData.verify_code, it will change input filed value. Commented Nov 5, 2014 at 3:16

1 Answer 1

0

You don't need to change input value by manupulate dom in angularjs. Angular already has a lot of tremendous mechanism to help you reduce your development work.

In your case, you just change your code into this:

.controller('AngularRegisterVerifyPage', function($scope, $routeParams){
     $scope.formData = {};
     if ($routeParams.hasOwnProperty("verify_code")) {
         //You change your value in your model, angular will render this to your html(view)
         $scope.formData.verify_code = $routeParams.verify_code;
     }

});

Here is a useful answer for angular newer: “Thinking in AngularJS” if I have a jQuery background?

Here is ngModel Document.

Enjoy it : )

Sign up to request clarification or add additional context in comments.

5 Comments

I get this error: TypeError: Cannot set property 'verify_code' of undefined
Then you need to initialize $scope.formData to an empty object at the top of your controller.
Sorry, my fault. Change your code into $scope.formData = {}; if ($routeParams.hasOwnProperty("verify_code")) { $scope.formData.verify_code = $routeParams.verify_code; }
@Tyler.z.yang Thank you this works. Angular is new to me, right now I got everything crammed into main.js I got to learn about how to organize it better. Thanks.
@Datsik You are welcome. This answer is for folder structure. I think this is helpful. Happy coding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.