2

I am trying to learn Angular JS with an HTML Sample. I would like the user to fill out some basic information, and based on the checkbox they select, it will load a form page using the UI Routing. It will generate links to navigate the page automatically, based on the checkboxes selected. Then, once the form is complete it should save in a directory on the server, and download to the user computer.

I got the form to show all data as json array, but now nothing is working after trying to add the ability to create the checklist links, as navigation, and saving?

App.js

create our angular app and inject ngAnimate and ui-router

angular.module('formApp', ['ngAnimate', 'ui.router'])

//configuring our routes 

.config(function ($stateProvider, $urlRouterProvider) {
$stateProvider

    // route to show our basic form (/form)
    .state('form', {
        url: '/form',
        templateUrl: 'form.html',
        controller: 'formController'
    })

    // nested states 
    // each of these sections will have their own view
    // url will be nested (/form/profile)
    .state('form.profile', {
        url: '/profile',
        templateUrl: 'form-profile.html'
    })

    // url will be /form/interests
    .state('form.interests', {
        url: '/interests',
        templateUrl: 'form-interests.html'
    })

    // url will be /form/payment
    .state('form.payment', {
        url: '/payment',
        templateUrl: 'form-payment.html'
    });

// catch all route
// send users to the form page 
$urlRouterProvider.otherwise('/form/profile'); })

// our controller for the form //
.controller('formController', function ($scope) {

// we will store all of our form data in this object

$scope.prefContactArray = [];
$scope.prefContactArray.push({ name: "Text", reply: "Great we'll text you.", isDefault: false });
$scope.prefContactArray.push({ name: "Email", reply: "Great we'll send you an email!", isDefault: false });
$scope.prefContactArray.push({ name: "Phone", reply: "Great we'll give you a call.", isDefault: false });

$scope.selectedprefContact = $scope.prefContactArray.name;
$scope.selectedprefContactReply = $scope.prefContactArray.reply;

 $scope.fruitsList = [
{ id: 1, name: 'Apple', url: 'form/profile.html', state:'.profile' },
{ id: 2, name: 'Banana', url: 'form/interests.html', state:'.interests' },
{ id: 3, name: 'Guava', url: 'form/payment.html', state:'payment' }
 ];


    $scope.selected = {
       fruitsList: []
    };

    $scope.checkAll = function () {
        $scope.selected.fruitsList = angular.copy($scope.fruitsList);
    };
    $scope.uncheckAll = function () {
        $scope.selected.fruitsList = [];
    };

    $scope.create = function () {
        var aTag = document.createElement('a ui-sref-active="active" ui-sref="fruitsList.state"
alt="fruitsList.name"');
        status-buttons.appendChild(aTag);
        $state.go($scope.selected.fruitsList.url);
    };

    $scope.formData = {};


   $scope.submit = function downloadFile(fileName, urlData) {
        var aLink = document.createElement('a');
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent("click");
        aLink.download = fileName;
        aLink.href = urlData;
        aLink.dispatchEvent(evt);
    }

   var data = $scope.formData;
    downloadFile('test.csv', 'data:text/csv;charset=UTF-8,' + encodeURIComponent(data));



});

Form.html

<div id="form-container">

    <div class="page-header text-center">
        <h2>Let's Be Friends</h2>

        <!-- the links to our nested states using relative paths -->
        <!-- add the active class if the state matches our ui-sref -->
        <div id="status-buttons" class="text-center">
            <a ui-sref-active="active" ui-sref=".profile"><span>1</span> Profile</a>
            <a ui-sref-active="active" ui-sref=".interests"><span>2</span> Interests</a>
            <a ui-sref-active="active" ui-sref=".payment"><span>3</span> Payment</a>
        </div>
    </div>
    <div id="splitscreen">
        <!-- use ng-submit to catch the form submission and use our Angular function -->
        <form id="signup-form" ng-submit="createQuote()">
            <div id="userPanel" class="col-md-6" style="background-color:#999; z-index:2;">
                <div class="form-group">
                    <label for="name">Name</label>
                    <input type="text" class="form-control" name="name" ng-model="formData.name">
                </div>

                <div class="form-group">
                    <label for="email">Email</label>
                    <input type="text" class="form-control" name="email" ng-model="formData.email">
                </div>

                <div class="form-group">
                    <label for="email">Phone</label>
                    <input type="text" class="form-control" name="email" ng-model="formData.phone">
                </div>

                <div class="form-group">
                    <label for="email">Website</label>
                    <input type="text" class="form-control" name="email" ng-model="formData.web">
                </div>

                <div ng-repeat="prefContact in prefContactArray">
                    <label>
                        <input type="radio" ng-value="prefContact.reply" ng-model="$parent.selectedprefContact" />
                        {{prefContact.name}}

                    </label>
                </div>{{selectedprefContact | json}}



                <div>
                    <label ng-repeat="fruit in fruitsList">
                        <input type="checkbox" checklist-model="selected.fruitsList" checklist-value="fruit.id"
ng-click="create()" /> {{fruit.name}}<br />
                    </label>
                    <button ng-click="checkAll()">Check all</button>
                    <button ng-click="uncheckAll()">Uncheck all</button> <br />
                    {{selected.fruitsList}}

                </div>

                </div>


                </div>
            <pre>
    {{ formData }}
</pre>
            <div id="questions" class="col-md-6">
                <!-- our nested state views will be injected here -->
                <div id="form-views" ui-view></div>
            </div> </form>
    </div>
</div>

<!-- show our formData as it is being typed -->

Submit Button Page

Thanks For Your Money!

<button type="submit" class="btn btn-danger">Submit</button> </div>

Index

<!-- CSS -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootswatch/3.1.1/darkly/bootstrap.min.css">
<link rel="stylesheet" href="style.css">

<!-- JS -->
<!-- load angular, nganimate, and ui-router -->
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular-animate.min.js"></script>
<script src="app.js"></script>

</head>

<!-- apply our angular app --> <body ng-app="formApp">

 <div class="container col-md-12">

    <!-- views will be injected here -->
    <div class="col-md-12" ui-view></div>

</div>

  • You may want to fix the markup in your post. The code now gets broken up into pieces which makes it very difficult to help you out. – Coo Nov 02 '15 at 01:42
  • @Coo I adjusted the markup, thanks! – 贝壳 - BeiKe Nov 02 '15 at 18:23
  • The thing that confuses me is this: you have the submit button outside of the form. Why is that a different page? – Coo Nov 03 '15 at 05:03
  • Because it is the last part of the form, and I have some inputs on the form template as well. – 贝壳 - BeiKe Nov 06 '15 at 03:57
  • I am currently trying to troubleshoot this error: An unknown WinINet error has occurred (code 12113). – 贝壳 - BeiKe Nov 06 '15 at 03:58
  • That looks like a firewall issue or something. Are you uploading the code all the time? Or are you using your laptop as a local server? – Coo Nov 06 '15 at 04:48
  • Originally I was using my laptop, but then I started making FTP updates. Any advice? I read it could be a firewall thing. Is that just visual studio? should I login somewhere else? – 贝壳 - BeiKe Nov 07 '15 at 19:01
  • For developing and testing, it's highly recommended to just use your laptop. Somewhere in the process the FTP connection is probably not quite doing what it should. By not using your own laptop for development you're just increasing the number of things that could go wrong. As in this case it has. The WinNet problem is something that I thing is nothing to do with the Angular Problem. – Coo Nov 08 '15 at 03:26

2 Answers2

0

In your create() function you use $state.go($scope.selected.fruitsList.url) which will change to the new state, however the value is the template path rather than the state path.

You should use $state.go($scope.selected.fruitsList.state) because the 'to' parameter of $state.go() should be the name of the state that will be transitioned to or a relative state path. If the path starts with ^ or . then it is relative, otherwise it is absolute.

andorov
  • 4,197
  • 3
  • 39
  • 52
Tristan
  • 3,301
  • 8
  • 22
  • 27
0

$state

As @Andorov already mentioned, you need $state to navigate. UI Router has offers this service to make it easy for you to go from one state (or route, or page) to another. Add the dependency to your controller like so:

.controller('formController', function ($scope, $state) {

You are now able to say something like $state.go('form.payment') in your controller. This will navigate the person to the Payment form.

So all you would need to do now is when they submit the form (i.e. inside your $scope.createQuote() function which you haven't included in the code yet), find out what state you should go to and end with $state.go(stateToGoto).

Tip:

When I started out with UI router and AngularJs, I just made every route its own page, not using children. If you would do that you would get:

  • A route for your form
  • A route for every page it could go to.

Every route has its own controller, which makes it easy to put code in the right place. I don't like sharing the controller between children as it just makes it more difficult to understand which part of the code is for which child.

Does this help?

Coo
  • 1,842
  • 3
  • 19
  • 37