5

Pardon my newbiness but I can't figure it out. Why is my $scope.new_prompt undefined when clicking submit form? I can see the variable being updated as I type (in <p>{{new_prompt}}</p>) but when I click submit, console logs 'undefined'.

View:

<div class="panel" ng-if="isAuthenticated()">
    <div class="panel-body">
        <h2 class="text-center">Add a prompt</h2>
        <form method="post" ng-submit="submitPrompt()" name="promptForm">
            <div class="form-group has-feedback">
                <input class="form-control input-lg" type="text" name="prompt" ng-model="new_prompt" placeholder="Imagine if..." required autofocus>
                <span class="ion-edit form-control-feedback"></span>
            </div>
            <p>{{new_prompt}}</p>
            <button type="submit" ng-disabled="promptForm.$invalid" class="btn btn-lg btn-block btn-success">Add prompt</button>
        </form>
    </div>
</div>

Controller:

angular.module('Prompts')
    .controller('HomeCtrl', ['$scope', '$auth', 'Prompt', '$alert', '$rootScope',
        function($scope, $auth, Prompt, $alert, $rootScope) {
            $scope.isAuthenticated = function() {
                return $auth.isAuthenticated();
            };
            $scope.submitPrompt = function() {
                console.log($scope.new_prompt);
                Prompt.submitPrompt({
                    idea: $scope.new_prompt,
                    user: $rootScope.user
                }).then(function() {
                    $alert({
                        content: 'Prompt has been added',
                        animation: 'fadeZoomFadeDown',
                        type: 'material',
                        duration: 3
                    });
                });
            };
            $scope.stories = [{
                prompt: 'Prompt 1',
                author: 'blank',
                upvotes: 0
            }, {
                prompt: 'Prompt 2',
                author: 'klanb',
                upvotes: 1
            }, {
                prompt: 'Prompt 3',
                author: 'balbunk',
                upvotes: 2
            }, ];
        }
    ]);

edit:

Ved's solution got it working. Now I don't understand why it had to be done like that when this works:

<div class="panel">
    <form ng-submit="printText()">
        <input type="text" ng-model="justTest">
        <button type="submit" class="btn"></button>
    </form>
</div>

$scope.printText = function() {
    console.log($scope.justTest)
};

Working example of edit: http://jsfiddle.net/fuczak/dLcLkycb/

EDIT 2:

The problem lies within ng-if directive. It creates own child scope, and that's where 'new_prompt' is located, not in the parent HomeCtrl scope.

4
  • Define $scope.new_prompt inside your controller. Commented Feb 4, 2015 at 9:40
  • Isn't it defined in input with ng-model="new_prompt" ? Commented Feb 4, 2015 at 9:44
  • no. You need to declare it as a $scope variable. Commented Feb 4, 2015 at 9:45
  • And that's why they say you should always bind to nested objects. You have to have a "." in every binding otherwise you risk binding to a scope you didn't know was there. Use controllerAs syntax if possible. Otherwise in your controller create one object on the scope and bind to attributes on that object. Commented Feb 4, 2015 at 16:28

1 Answer 1

5

There are two ways to solve your mistake.
case 1: Pass your model as a parameter to function:

HTML:

 <form method="post" ng-submit="submitPrompt(new_prompt)" name="promptForm">

JavaScript:

 $scope.submitPrompt = function(data) {
       $scope.new_prompt=data;
                console.log($scope.new_prompt);
                Prompt.submitPrompt({
                    idea: $scope.new_prompt,
                    user: $rootScope.user
                }).then(function() {
                    $alert({
                        content: 'Prompt has been added',
                        animation: 'fadeZoomFadeDown',
                        type: 'material',
                        duration: 3
                    });
                });
            };

CASE 2: Define: scope.new_prompt= {}, inside your controller

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

3 Comments

Thank you, it worked. Can you please look at my edit and help me out with understanding it?
@user3506265 I saw your update. It will not work as you are saying. Show with working example.
The link to the example is broke!

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.