Nurdin
Nurdin

Reputation: 23883

ng-model value undefined

I got undefined inside my ng-model. I already defined inside my controller. $scope.rule.message no issue but for $scope.rule.ruleId got undefined.

html

<form>
    <div class="form-group">
        <label>Group</label> <select ng-model="rule.ruleId" ng-repeat="rule in rules">
            <option value="{{rule.ruleId}}">
                #{{rule.keyword}}
            </option>
        </select>
    </div>


    <div class="form-group">
        <label>Message</label> 

        <textarea class="form-control" cols="" name="" rows="" ng-model="rule.message">
</textarea>
    </div>


    <div class="form-group">
        <button class="btn btn-default" type="submit" ng-click="create()">Save</button>
    </div>
</form>

js

mzsmsControllers.controller('MemberBlastCreateCtrl', ['$scope', '$http',
    '$location',
    function($scope, $http, $location) {
        $scope.rule = {};
        $http({
            method: 'GET',
            url: 'http://example.com/get_all_rules.php?fbId=' + sessionStorage.id
        }).
        success(function(data, status, headers, config) {
            $scope.rules = data.rules;
        }).
        error(function(data, status, headers, config) {
            alert("No internet connection.");
        });
        $scope.create = function() {
            $http({
                method: 'GET',
                url: 'http://example.com/create_blast.php?fbId=' + sessionStorage.id +
                    '&ruleId=' + $scope.rule.ruleId + '&message=' + $scope.rule.message
            }).
            success(function(data, status, headers, config) {
                alert(JSON.stringify(data));
                alert("SMS Blast successfuly created.");
                $location.path("/member/blast");
            }).
            error(function(data, status, headers, config) {
                alert("No internet connection.");
            });
        }
    }
]);

Upvotes: 0

Views: 189

Answers (1)

frankfg
frankfg

Reputation: 625

I guess the problem is that ng-repeat creates a new scope, try changing the ng-repeat:

<select ng-model="rule.ruleId" ng-repeat="r in rules">
        <option value="{{r.ruleId}}">
            #{{r.keyword}}
        </option>
    </select>

Upvotes: 1

Related Questions