-4

Pulling values from a form in AngularJS doesn't appear to be working - for example my form code is:

<form ng-submit="newCompany()">
    <div class="form-group">
        <label>Name</label><input type="text" name="company_name" id="company_name" tabindex="2" ng-model="newCompany.company_name" class="form-control">
        <label>Primary Contact</label><input type="text" name="primary_contact" id="primary_contact" tabindex="2" ng-model="newCompany.primary_contact" class="form-control">
        <label>Address</label><input type="text" name="address" id="address" tabindex="2" ng-model="newCompany.address" class="form-control">
        <label>Function</label><input type="text" name="function" id="function" tabindex="2" ng-model="newCompany.function" class="form-control">
        <label>Telephone</label><input type="text" name="telephone" id="telephone" tabindex="2" ng-model="newCompany.phone" class="form-control">
        <label>Fax</label><input type="text" name="fax" id="fax" tabindex="2" ng-model="newCompany.fax" class="form-control">
        <label>URL</label></label><input type="text" name="url" id="url" tabindex="2" ng-model="newCompany.url" class="form-control">
    </div>
    <div class="form-group">
        <div class="row">
            <div class="col-sm-6 col-sm-offset-3">
                <input type="submit" name="add-submit" id="add-submit" tabindex="10" class="form-control btn btn-primary" value="Add Company">
                <br>
                <div class="text-center">
                    <p ng-show="addCompany"><span class="label label-info">{{ addCompany }}</span></p>
                </div>
            </div>
        </div>
    </div>
</form>

But in the controller, doing newCompany.name or $scope.newCompany.name, doesn't work. It returns nothing and says newCompany is undefined.

Controller:

app.controller("CompaniesController", ['$scope', 'Companies', function($scope, Companies) {
    $scope.title = 'Companies';
    $scope.title_sub = 'Add Company';

   $scope.companyData = {
       company_name: newCompany.company_name,
       primary_contact: newCompany.primary_contact,
       address: newCompany.address,
       function: newCompany.function,
       telephone: newCompany.phone,
       fax: newCompany.fax,
       url: newCompany.url
   };

    $scope.addCompany = function() {
        var company = new Companies($scope.companyData);
        company.$save();
    };

    $scope.companies = Companies.query();
}]);

Can anybody see what is wrong or how to get it to work?

3
  • You said, when the form is submitted call the function newCompany(). I don't see that function. Commented Nov 19, 2015 at 12:54
  • You try to call newCompany() which is not defined. Your model is newCompany... so which it is, object or a function? also you try to use ng-show="addCompany" but addCompany is a function. Maybe you should re-read & debug your code just once more... Commented Nov 19, 2015 at 12:57
  • 1
    stackoverflow.com/questions/33802005/… It is you again. Try to finish all the steps I suggested there, then if it is still not working post here. Commented Nov 19, 2015 at 13:03

1 Answer 1

0

Yes newCompany is undefined please define it before use will solve your problem

 $scope.newCompany = {};

   $scope.companyData = {
       company_name: newCompany.company_name,
       primary_contact: newCompany.primary_contact,
       address: newCompany.address,
       function: newCompany.function,
       telephone: newCompany.phone,
       fax: newCompany.fax,
       url: newCompany.url
   };

and also you don't need to use $scope.companyData just use newCompany inside your save function

$scope.addCompany = function() {
        var company = new Companies($scope.newCompany);
        company.$save();
    };

if you see in console using console.log(newCompany) after submit data it already there with key value pair as you trying to create with your $scope.companyData

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

2 Comments

I've just done that and it still claims it is undefined. $scope.newCompany = {}; $scope.companyData = { company_name: newCompany.company_name, primary_contact: newCompany.primary_contact, address: newCompany.address, function: newCompany.function, telephone: newCompany.phone, fax: newCompany.fax, url: newCompany.url };
please add more code in your question or better create a fiddle will help us

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.