Angular password generator service
<p>I have a password generator and I was using it in two different controllers so I thought I should move it into a service. </p>

<ol>
<li>I am not 100% sure a service is the best approach and would like to know what is if it is not.</li>
<li>I am running into one problem. The service sends back a passWord object when I update the password input field I have to use <code>ng-model="passWord.password"</code>. </li>
</ol>

<p>Here is the problem:</p>

<p>Because the form has other fields that use <code>user.VALUE</code> I need to have my password field read <code>ng-model="user.password"</code> but this is breaking in the service. Below is the code.</p>

<p>Service:</p>

<pre><code>app.factory('PassWord', function($resource, $log, $rootScope, $location, $sessionStorage, $q, Data){

    var passWord = {
        inputType:"password",
        showPwTxt:"Show"
    };

    // Hide &amp; show password function
    passWord.hideShowPassword = function(){
        if (passWord.inputType == 'password') {
            type = 'text';
            txt = 'Hide';
        }
        else {
          type = 'password';
          txt = 'Show';
        }

        passWord.inputType = type;
        passWord.showPwTxt = txt;

    };

    // test for medium and strong passwords
    var strongRegex = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[-!@#\"\$%\^&amp;\*()_+|=\\]\\[{}~`:\";\'&lt;&gt;?,.\/])(?=.{8,})");
    var mediumRegex = new RegExp("^(((?=.*[a-z])(?=.*[A-Z]))|((?=.*[a-z])(?=.*[0-9]))|((?=.*[A-Z])(?=.*[0-9])))(?=.{6,})");

    // create an object for the pw strength
    passWord.passwordStrength = {};

    passWord.analyze = function(value) {
        if(strongRegex.test(value)) {
            color = "#57A83F";
            txt = "Strong";
        } else if(mediumRegex.test(value)) {
            color = "#FF9800";
            txt = "Medium";
        } else {
            color = "#E03930";
            txt = "Weak";
        }

        passWord.passwordStrength["color"] = color;
        passWord.pwStrengthTxt = txt;

    };

    // password generator
    passwordLength = 12;
    addUpper       = true;
    addNumbers     = true;
    addSymbols     = true;

    passWord.createPassword = function(){
        var lowerCharacters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
        var upperCharacters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
        var numbers = ['0','1','2','3','4','5','6','7','8','9'];
        var symbols = ['!', '"', '#', '$', '%', '&amp;', '\'', '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '&lt;', '=', '&gt;', '?', '@', '[', ']', '^', '_', '`', '{', '|', '}', '~'];
        var finalCharacters = lowerCharacters;
        //if(passWord.addUpper){
            finalCharacters = finalCharacters.concat(upperCharacters);
        //}
        //if(passWord.addNumbers){
            finalCharacters = finalCharacters.concat(numbers);
        //}
        //if(passWord.addSymbols){
            finalCharacters = finalCharacters.concat(symbols);
        //}
        var passwordArray = [];
        for (var i = 1; i &lt; passwordLength; i++) {
            passwordArray.push(finalCharacters[Math.floor(Math.random() * finalCharacters.length)]);
        };

        //update the passwrod input field in the form
        passWord.password = passwordArray.join("");

        //analyze thepassword strength
        passWord.analyze(passWord.password);

        if(passWord.pwStrengthTxt == "Medium" || passWord.pwStrengthTxt == "Weak") {
            passwordArray = [];
            passWord.createPassword();
        }
    };

    return passWord;
});
</code></pre>

<p>HTML:</p>

<pre><code>&lt;form name="resetForm" class="" role="form"&gt;

    &lt;div class="form-group row" ng-show="!uid"&gt;

        &lt;div class="col-sm-12"&gt;

            &lt;input type="email" class="form-control" placeholder="Email" name="email" ng-model="user.email" ng-required="!uid"/&gt;

            &lt;span ng-show="!resetForm.email.$error.required &amp;&amp; !resetForm.email.$error.email" class="glyphicon glyphicon-ok form-control-feedback" &gt;&lt;/span&gt;

            &lt;span ng-show="resetForm.email.$touched"&gt;
                &lt;small class="errorMessage" data-ng-show="resetForm.email.$error.required"&gt;Email is required.&lt;/small&gt;
            &lt;/span&gt;

            &lt;small class="errorMessage" ng-show="resetForm.email.$error.email"&gt;Email is not valid&lt;/small&gt;

        &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="form-group row" ng-hide="!uid"&gt;
        &lt;div class="col-sm-12"&gt;
            &lt;span class="block input-icon input-icon-right"&gt;
                &lt;div class="input-group"&gt;
                &lt;input type="{{passWord.inputType}}" class="form-control" name="password" placeholder="Password" ng-minlength="6" ng-model="passWord.password" ng-change="passWord.analyze(passWord.password)" ng-required="uid"/&gt;
                &lt;span id="spnPassword" class="add-on input-group-addon" ng-click="passWord.hideShowPassword()" style="cursor:pointer;"&gt;{{passWord.showPwTxt}}&lt;/span&gt;
                &lt;/div&gt;
                &lt;span ng-hide="resetForm.password.$error.required || resetForm.password.$error.minlength" class="glyphicon glyphicon-ok form-control-feedback" &gt;&lt;/span&gt;
                &lt;small class="errorMessage" ng-show="resetForm.password.$error.minlength"&gt;Too short! Minimum 6 characers&lt;/small&gt;
            &lt;/span&gt;
            &lt;div ng-show="passWord.password.length"&gt;
                &lt;small&gt;Password strength: &lt;span ng-style="passWord.passwordStrength"&gt;{{passWord.pwStrengthTxt}}&lt;/span&gt;&lt;/small&gt;
            &lt;/div&gt;
            &lt;br&gt;
            &lt;button type="button" class="btn btn-sm btn-primary" data-ng-click="passWord.createPassword()"&gt;Generate Password&lt;/button&gt;
        &lt;/div&gt;
    &lt;/div&gt;

    &lt;div class="clearfix"&gt;
        &lt;div class="row"&gt;

            &lt;span class="lbl col-sm-7"&gt;&lt;span ng-show="!uid"&gt;Remembered your password? &lt;a href="login"&gt;SignIn&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;

            &lt;div class="col-sm-5 text-right"&gt;
                &lt;button type="submit" class="btn btn-info" ng-click="resetPassword(user)" data-ng-disabled="resetForm.$invalid"&gt;Reset Password&lt;/button&gt;
            &lt;/div&gt;

        &lt;/div&gt;
    &lt;/div&gt;

&lt;/form&gt;
</code></pre>

<p>CONTROLLER:</p>

<pre><code>app.controller('resetCtrl', function ($scope, $rootScope, $log, $routeParams, $location, $http, Data, PassWord) {

    $scope.user = {email:'',password:''};

    $scope.passWord = PassWord;

    $scope.resetPassword = function (user) {
        $log.log('user is - ',user);
        // Do Stuff
    };

});
</code></pre>