Angular ngDialog not Displaying model data in $scope - angularjs

Continually having issues with ng-dialog properly displaying model data. My app uses two ng-dialog instances, one to add data (in this case for a League) and a second dialog to edit data. Both use the the same ng-model data. When add League is selected, the first dialog pops up, the user populates the fields, and saves the record, the dialog is closed and the main page displays the created League along with its data. An EDIT button is presented.
The issue in this case occurs when the EDIT button is pressed.
The controller's openEditLeague method is called, which prepopulates the leagueForm with the data for the league I am trying to edit.
'use strict';
angular.module('ma-app')
.filter('trustUrl', function ($sce) {
return function(url) {
return $sce.trustAsResourceUrl(url);
};
})
.controller('HomeController', ['$scope', 'ngDialog', 'authService', 'coreDataService', 'userService', '$rootScope', 'clubService', 'schedulingService', function($scope, ngDialog, authService, coreDataService, userService, $rootScope, clubService, schedulingService) {
....
$scope.leagueForm = {
name: '',
shortname: '',
minAgeGroup: null,
maxAgeGroup: null,
type: null,
rescheduleDays: '',
consequence: '',
fine: '',
logoURL: '',
leagueId: ''
};
....
$scope.openEditLeague = function(league) {
console.log("\n\nOpening dialog to edit league");
console.log(league);
$scope.leagueForm = {
name: league.name,
shortname: league.short_name,
minAgeGroup: league.min_age_group,
maxAgeGroup: league.max_age_group,
type: league.type,
rescheduleDays: league.reschedule_time,
consequence: league.reschedule_consequence,
fine: league.reschedule_fine,
logoURL: league.logo_url
};
console.log("Current entries include: ");
console.log($scope.leagueForm);
ngDialog.open({ template: 'views/editLeague.html', scope: $scope, className: 'ngdialog-theme-default custom-width-600', controller:"HomeController" });
};
....
}])
;
I am logging the contents of the $scope.leagueForm prior to opening the ng-dialog, and the data contents are correct.
When the dialog opens however, all fields are empty.
This should be fairly straight forward data-binding, so I must be doing something wrong.
Here are the contents of the editLeague.html that are used to generate the ng-dialog:
<div class="ngdialog-message">
<div>
<h3>Edit League</h3>
</div>
<div> </div>
<div>
<form class="form-horizontal" ng-submit="editLeague()">
<div class="form-group">
<label class="sr-only" for="name">League Name</label>
<div class="col-xs-12 col-sm-6">
<input type="text" class="form-control" id="name" placeholder="league name" ng-model="leagueForm.name">
</div>
<label class="sr-only" for="shortname">League Abbreviation</label>
<div class="col-xs-12 col-sm-6">
<input type="text" class="form-control" id="shortname" placeholder="league abbreviation" ng-model="leagueForm.shortname">
</div>
</div>
<div class="form-group">
<label class="sr-only" for="minage">Minimum Age Group</label>
<div class="col-xs-12 col-sm-6">
<div class="input-group">
<div class="input-group-addon">Min Age Group</div>
<select id="minage" class="form-control" ng-model="leagueForm.minAgeGroup" ng-options="ageGroup as ageGroup.name for ageGroup in ageGroups"></select>
</div>
</div>
<label class="sr-only" for="maxage">Maximum Age Group</label>
<div class="col-xs-12 col-sm-6">
<div class="input-group">
<div class="input-group-addon">Max Age Group</div>
<select id="maxage" class="form-control" ng-model="leagueForm.maxAgeGroup" ng-options="ageGroup as ageGroup.name for ageGroup in ageGroups"></select>
</div>
</div>
</div>
<div class="form-group">
<label class="sr-only" for="type">League Type</label>
<div class="col-xs-12 col-sm-6">
<div class="input-group">
<div class="input-group-addon">Type</div>
<select id="type" class="form-control" ng-model="leagueForm.type" ng-options="leagueType as leagueType.name for leagueType in leagueTypes"></select>
</div>
</div>
<label class="sr-only" for="days">Day to Reschedule</label>
<div class="col-xs-12 col-sm-6">
<div class="input-group">
<input type="text" class="form-control" id="days" placeholder="days to reschedule" ng-model="leagueForm.rescheduleDays">
<div class="input-group-addon">Days</div>
</div>
</div>
</div>
<div class="form-group">
<label class="sr-only" for="consequence">Missed Reschedule Date Consequence</label>
<div class="col-xs-12 col-sm-6">
<div class="input-group">
<div class="input-group-addon">Consequence</div>
<select id="consequence" class="form-control" ng-model="leagueForm.consequence">
<option value =""></option>
<option value="NONE">N/A</option>
<option value="FORFEIT">Forfeit</option>
<option value="FINE">Fine</option>
</select>
</div>
</div>
<div class="col-xs-12 col-sm-6">
<div class="input-group" ng-show="!fineHidden">
<label class="sr-only" for="fine">Fine</label>
<div class="input-group-addon">$</div>
<input type="text" class="form-control" id="fine" placeholder="fine" ng-model="leagueForm.fine">
<div class="input-group-addon">.00</div>
</div>
</div>
</div>
<button type="submit" class="btn btn-info">Update</button>
<button type="button" class="btn btn-default" ng-click=closeThisDialog("Cancel")>Cancel</button>
</form>
</div>
<div> </div>
</div>

I think it may be due to you passing the controller value as HomeController - which is overwriting the leagueForm value (to empty values) in the $scope as it's spun up.

Resolved. Was very simple after all. Just needed to remove the empty definition of
$scope.leagueForm = {
name: '',
shortname: '',
minAgeGroup: null,
maxAgeGroup: null,
type: null,
rescheduleDays: '',
consequence: '',
fine: '',
logoURL: '',
leagueId: ''
};
from the controller. Then reference leagueForm.name, etc, as the ng-model in the Add dialog, and finally my $scope.openEditLeague function works as it is specified in the original post.

Related

AngularJS: Some form fields not bound with viewmodel

I'm writing a form that binds inputs with a angularJS model. For some reason, only certain fields are bound to the model (vm.customer). For example, vm.last_name, and vm.email are bound. But vm.first_name and vm.gender are not bound from inputs to model.
/* AddCustomer.js */
(function (angular) {
'use strict';
angular.module('app', []);
function controller($scope) {
var vm = this;
vm.genders = ["Male", "Female", "Other"];
vm.customer = {
first_name: 'Susan',
last_name: 'BOyle',
email: 's.boyle#singwell.com',
ip_address: '192.168.1.120',
gender: vm.genders[1]
}
vm.addCustomer = function($scope) {
console.log("bout to add a user");
};
vm.$onInit = function() {
};
}
// dep. injecion
controller.$inject = ['$scope'];
angular
.module('app')
.controller('AddCustomer', controller);
})(window.angular);
This is the html file
/* add-customer-view.html */
<form ng-app="app" ng-controller="AddCustomer as vm">
<pre>
customer = {{ vm.customer | json }}
</pre>
<div class="form-row">
<div class="form-group col-md-6">
<label>First</label>
<input class="form-control" type="text" ng-model"vm.customer.first_name" ng-model-options="{ updateOn: 'blur' }">
</div>
<div class="form-group col-md-6">
<label>Last</label>
<input class="form-control" type="text" ng-model="vm.customer.last_name">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label class="col-form-label">Email</label>
<input class="form-control" type="email" ng-model="vm.customer.email">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<div class="form-check form-check-inline" ng-repeat="gender in vm.genders">
<input type="radio" name="gender" ng-model"vm.customer.gender" value="vm.genders[{{$index}}]">
<label class="form-check-label">{{ gender }}</label>
</div>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label class="col-form-label">IP Address</label>
<input class="form-control" type="text" ng-model="vm.customer.ip_address">
</div>
</div>
<div class="form-row">
<button type="submit" class="btn btn-primary" ng-click="vm.addCustomer()">Add Customer</button>
</div>
Link to code segments: https://codepen.io/nmnduy/pen/ypvddZ. Any insight would be very helpful!
1) You miss = operator on both cases (first_name, gender)
2) You need to replace input's value as follows:
<input type="radio" name="gender" ng-model="vm.customer.gender" value="{{gender}}">

Angularjs bad argument ng:areq error

I'm getting this error when running my application. This error comes in only one controller. I have done other controllers in the same way but in this particular controller I get this following error.
Argument 'questionAddCtrl' is not a function, got undefined
Controller:
;
(function () {
'use strict';
angular.module('app').controller('questionAddCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.data = {
question: '',
ans1: '',
ans2: '',
ans3: '',
ans4: '',
ans5: '',
correct_ans: ''
};
$scope.submit = function (selectData) {
console.log("submit pressed");
var questionAddRequest = {
"question": selectData.question,
"answerOne": selectData.ans1,
"answerTwo": selectData.ans2,
"answerThree": selectData.ans3,
"answerFour": selectData.ans4,
"answerFive": selectData.ans5,
"correctAnswer": selectData.correct_ans
};
var url = 'http://localhost/AwtCW2012002/api/restApiController/question.json';
$scope.jsonData = JSON.stringify(questionAddRequest);
console.log(questionAddRequest);
console.log();
$http({
method: 'POST',
url: url,
data: questionAddRequest
}).then(function (response) {
console.log(response);
$scope.data = {
question: '',
ans1: '',
ans2: '',
ans3: '',
ans4: '',
ans5: '',
correct_ans: ''
};
});
}
}]);
})();
Views:
<div class="container" ng-controller="questionAddCtrl">
<form class="form-horizontal" role="form" name='quizAdd' ng-submit="submit(data)">
<div class="form-group">
<label class="control-label col-sm-2" for="question">Question:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="question" ng-model="data.question" placeholder="Enter Question">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="answer1">Answer 1:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="answer1" ng-model="data.ans1" id="answer1" placeholder="Enter Answer 1">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="answer2">Answer 2:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="answer2" ng-model="data.ans2" id="answer2" placeholder="Enter Answer 2">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="answer3">Answer 3:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="answer3" ng-model="data.ans3" id="answer4" placeholder="Enter Answer 3">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="answer4">Answer 4:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="answer4" id="answer4" ng-model="data.ans4" placeholder="Enter Answer 4">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="answer5">Answer 5:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="answer5" id="answer5" ng-model="data.ans5" placeholder="Enter Answer 5">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="sel1">Select Correct Answer:</label>
<div class="col-sm-10">
<select class="form-control" ng-model="data.correct_ans" id="sel1">
<option>{{data.ans1}}</option>
<option>{{data.ans2}}</option>
<option>{{data.ans3}}</option>
<option>{{data.ans4}}</option>
<option>{{data.ans5}}</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>
</div>
Try to set the array of dependencies that your module requires, like so:
angular.module('app', []);
instead of
angular.module('app');

angularjs: Form entry problems

I have this dynamic form which entries I would like to save. The problem is that the saved entry is like this [{"name":"Noora","":{"Gender":"Woman","Address":"filler address"}}] I'm curios why the app saves the info after name like a nameless list. Name is hardcoded input and the other two (Gender and Address) can be dynamically added when using the program.
Here is the form entry html part:
<form>
<h2>Sign in:</h2>
<div class="form-group">
<label for="eventInput">Name</label>
<input style="width: 200px;" type="text" class="form-control" id="eventInput" data-ng-model="newEntry.name">
</div>
<div data-ng-repeat="field in event.fields">
<div class="form-group">
<label for="{{$index + 1}}">{{field.name}}</label>
<input style="width: 200px;" type="text" class="form-control" id="{{$index + 1}}" data-ng-model="newEntry.[field.name]">
</div>
</div>
<span>{{entries}}</span>
<div class='wrapper text-center'>
<div class="form-group">
<div class="col-lg-4 col-lg-offset-4">
<button style="width: 100px;" data-ng-click="addEntry()" class="btn btn-primary">Enroll</button>
<p></p>
<button style="width: 100px;" data-ng-click="back()" class="btn btn-primary">Back</button>
</div>
</div>
</div>
</form>
and here is the controller:
App.controller('aboutController', function($scope, $location, $routeParams, eventService) {
$scope.event = eventService.getCustomers()[$routeParams.id];
$scope.back = function() {
$location.path('/');
};
$scope.addEntry = function() {
$location.path('/');
$scope.event.entries.push($scope.newEntry);
};
});
I would like to either be able to name the child list or just record the entries into a continuous list. Any idea how would it be possible?
Br,
Norri

AngularJS refresh smart table after inserting new record in Bootstrap Modal Form

I use angular-bootstrap and and Angular Smart table.
I have a modal form which adds new records to database and shows the list in my smart table. Adding new records is working fine but how do I refresh my smart table after a new record is inserted into the database.
Controller
//ajax request to gather data and list in smart table
october.controllers['dashboard/feeds'] = function ($scope, $filter , $request, $modal, $log, $http) {
$scope.api = $request('onFeeds', {success: function(data, scope){
this.success(data).done(function() {
$scope.rowCollection = [];
$scope.rowCollection = angular.fromJson(data.result);
$scope.displayedCollection = [].concat($scope.rowCollection);
});
}
});
}
//Modal
var ModalInstanceCtrl = function ($scope, $modalInstance, $request, $filter) {
$scope.save = function (data) {
$request('onAddFeed',
{data:data,
success: function(data){
this.success(data);
$scope.refresh();
$modalInstance.close();
}
});
};
};
Template
<script type="text/ng-template" id="myModalContent.html">
<div class="modal-header">
<h3 class="modal-title">neue Feed eintragen!</h3>
</div>
<div class="modal-body">
<form class="form-horizontal" role="form">
<input type=hidden ng-init="formInfo.user_id = {% endverbatim %} {{ user.id }} ">
{%verbatim %}
<div class="form-group">
<label for="inputName3" class="col-sm-2 control-label">Feed Name</label>
<div class="col-sm-8">
<input class="form-control" id="inputName3" placeholder="Feed Name" ng-model="formInfo.name">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Feed Type</label>
<div class="col-sm-8">
<select ng-model="formInfo.feed_type" class="form-control">
<option>Select Feed Source</option>
<option value="api">API</option>
<option value="xml">XML</option>
<option value="csv">CSV</option>
<option value="json">JSON</option>
<option value="upload">Upload</option>
</select>
</div>
</div>
<div class="form-group" ng-show="formInfo.feed_type =='api'">
<label for="selectAPI" class="col-sm-2 control-label">Select API</label>
<div class="col-sm-8">
<select ng-change="selectAction(item.id)" ng-model="formInfo.network_id" ng-options="item.id as item.name for item in networks" class="form-control"> </select>
</div>
</div>
<div class="form-group" ng-repeat="setup in setups">
<label for="setup" class="col-sm-2 control-label">{{setup}}</label>
<div class="col-sm-8">
<input ng-model="formInfo['api_credentials'][setup]" class="form-control" placeholder="Enter your {{setup}}">
</div>
</div>
<div class="form-group" ng-show="formInfo.feed_type =='xml' || formInfo.feed_type =='csv' ">
<label for="inputPassword3" class="col-sm-2 control-label">Source</label>
<div class="col-sm-8">
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-link"></i></div>
<input ng-model="formInfo.source" type="text" class="form-control" id="sourceInput" placeholder="URL">
</div>
</div>
</div>
</div>
<span>{{formInfo}}</span>
</form>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary" ng-click="save(formInfo)">Save</button>
<button type="button" class="btn btn-warning" ng-click="cancel()">Cancel</button>
</div>
</script>
{data:data,
success: function(data){
this.success(data);
$scope.refresh();
$modalInstance.close();
}
In the above code Instead of $scope.refresh() use $route.reload() to
Update the records before closing Modal Instance.
reload() Causes $route service to reload the current route even if $location hasn't changed
To refresh the smart-table you can add or remove items or just recreate the array.
$scope.tableData = [].concat($scope.tableData);
The thing that worked for me (and appears to be causing you the problem) is to set displayedCollection to an empty array. Smart Table will fill it with whatever paged, filtered, or sorted data you designate. Something like this:
<table st-table="displayedCollection" st-safe-src="rowCollection">
All of a sudden, rowCollection.push(data) automatically updated the Smart Table.

Multiple controllers in a meanjs form, submitting empty values

I am calling some values from the database and putting them in a select box in a form, however, whenever i click on submit, it submits an empty value, i am thinking its because i am using multiple controllers in the form, from what i have gathered, i have to do something with scope in the controllers, but i have been unable to do that
Attached is a copy of the create-view file, the highlited portions are the multiple controllers.
Please how do i make it work? Thank you very much
<section data-ng-controller="CandidatesController">
<div class="page-header">
<h1>New Candidate</h1>
</div>
<div class="col-md-12">
<form class="form-horizontal" data-ng-submit="create()" novalidate>
<fieldset>
<div class="form-group">
<label class="control-label" for="name">Name</label>
<div class="controls">
<input type="text" data-ng-model="name" id="name" class="form-control" placeholder="Name" required>
</div>
</div>
<div class="form-group">
<label class="control-label" for="vision">Vision</label>
<div class="controls">
<textarea data-ng-model="vision" id="vision" class="form-control"></textarea>
</div>
</div>
<div class="form-group">
<label class="control-label" for="dob">Date Of Birth</label>
<div class="controls">
<input type="date" data-ng-model="dob" id="dob" class="form-control" required>
</div>
</div>
<div class="form-group">
<label class="control-label" for="post">Post</label>
<div class="controls">
<select data-ng-model="post" id="post" class="form-control">
<option value="Presidential">PRESIDENTIAL</option>
<option value="Provincial">PROVINCIAL</option>
<option value="Municipal">MUNICIPAL</option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label" for="province">Province</label>
<div class="controls">
<select data-ng-model="province" id="province" class="form-control">
<option value="Gauteng">Gauteng</option>
<option value="Free-State">Free-State</option>
<option value="Kwazulu-Natal">Kwazulu-Natal</option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label" for="municipal">Municipal</label>
<div class="controls">
<input type="text" data-ng-model="municipal" id="municipal" class="form-control" placeholder="municipal" required>
</div>
</div>
<div class="form-group">
<label class="control-label" for="party">Party</label>
<div class="controls">
<section data-ng-controller="PartiesController" data-ng-init="find()">
<select data-ng-model="party" class="form-control" ng-options="party.name for party in parties track by party.name">
</select>
</section>
</div>
</div>
<div class="form-group">
<input type="submit" class="btn btn-default">
</div>
<div data-ng-show="error" class="text-danger">
<strong data-ng-bind="error"></strong>
</div>
</fieldset>
</form>
</div>
</section>
The candidate controller code
'use strict';
//Candidates controller
angular.module('candidates').controller('CandidatesController', ['$scope', '$stateParams', '$location', 'Authentication', 'Candidates',
function($scope, $stateParams, $location, Authentication, Candidates ) {
$scope.authentication = Authentication;
// Create new Candidate
$scope.create = function() {
// Create new Candidate object
var candidate = new Candidates ({
name: this.name,
vision: this.vision,
dob: this.dob,
post: this.post,
province: this.province,
municipal: this.municipal,
party: this.party
});
// Redirect after save
candidate.$save(function(response) {
$location.path('candidates/' + response._id);
// Clear form fields
$scope.name = '';
}, function(errorResponse) {
$scope.error = errorResponse.data.message;
});
};
// Remove existing Candidate
$scope.remove = function( candidate ) {
if ( candidate ) { candidate.$remove();
for (var i in $scope.candidates ) {
if ($scope.candidates [i] === candidate ) {
$scope.candidates.splice(i, 1);
}
}
} else {
$scope.candidate.$remove(function() {
$location.path('candidates');
});
}
};
// Update existing Candidate
$scope.update = function() {
var candidate = $scope.candidate ;
candidate.$update(function() {
$location.path('candidates/' + candidate._id);
}, function(errorResponse) {
$scope.error = errorResponse.data.message;
});
};
// Find a list of Candidates
$scope.find = function() {
$scope.candidates = Candidates.query();
};
// Find existing Candidate
$scope.findOne = function() {
$scope.candidate = Candidates.get({
candidateId: $stateParams.candidateId
});
};
}
]);

Resources