I'm new to both AngularJs and ngDialog, and I'm having trouble getting my bindings to work between the ngDialog modal and my controller. I injected the controller's scope into the modal by specifying { scope: $scope }
, and I have access to methods defined in the controller, but the bindings to models defined in the controller aren't functioning properly.
I'm trying to use a modal to allow the user to add an address to an organization.
Here's main.js
var App = angular.module('App', ['ngRoute', 'ngCookies', 'ngDialog']);
...
App.controller('PageOrganization', function($scope, $rootScope, ngDialog, $route, $location){
$scope.addAddressFormData = {};
$scope.addAddress = function(){
ngDialog.open({
template: 'partials/modals/add-address.html',
controller: 'PageOrganization',
scope: $scope
});
};
$scope.saveAddress = function(){
console.log($scope.addAddressFormData);
$scope.organization.addresses.push($scope.addAddressFormData);
console.log($scope.organization);
};
// STUBBED OUT ORGANIZATION
$scope.organization = {
org_type: "nonprofit",
name: 'New Organization',
addresses: [],
phoneNumber: "",
faxNumber: "",
emailAddress: "",
website: "",
primaryContact: "",
primaryEmail: "",
imageUrl: "",
isPrivate: false,
campaigns: [],
admins: []
};
Here's organization.html:
...
<button ng-click="addAddress()">Add an Address</button>
<h1>Addresses</h1>
<ul>
<li ng-repeat="address in organization.addresses">
<p>
{{address.type}} <br>
{{address.addressLine1}} <br>
{{address.addressLine2}} <br>
{{address.city}} <br>
{{address.state}} <br>
{{address.postalCode}}
</p>
</li>
</ul>
And here's add-address.html:
<h1>Add an Address</h1>
<form ng-submit="saveAddress()">
<select name="type">
<option value="business" default="selected">Business</option>
<option value="residence">Residence</option>
</select>
<input ng-model="addAddressFormData.addressLine1" type="text" placeholder="Address Line 1">
<input ng-model="addAddressFormData.addressLine2" type="text" placeholder="Address Line 2">
<input ng-model="addAddressFormData.city" type="text" placeholder="City">
<select ng-model="addAddressFormData.state">
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">District Of Columbia</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>
<input ng-model="addAddressFormData.postalCode" type="text" placeholder="Postal Code">
<input type="submit" value="Save Address">
</form>
The modal has access to the parent scope; it successfully calls $scope.saveAddress()
and console.log($scope.organization)
logs the whole organization, including the new address added in the modal. However, the new address is not reflected in the ng-repeat
in organization.html, and if I add multiple addresses one after another, only the most recent one shows in the log.
As an experiment, I added this function to main.js:
$scope.pushAddress = function(){
$scope.addAddressFormData = {city: $scope.organization.addresses.length + 1};
console.log($scope.addAddressFormData);
$scope.organization.addresses.push($scope.addAddressFormData);
console.log($scope.organization);
};
and changed the "Add Address" button in organization.html to match:
<button class="button color-c vertical-a float-right" ng-click="pushAddress()">Add an Address</button>
Now when I click "Add an Address," the new address shows immediately in the ng-repeat
, and each console log contains all of the addresses, not just the most recent one.
What's the difference between these two methods? Why are the changes made in the modal 'expiring' when they were made using methods in the controller's scope?