我是新来的AngularJs和
ngDialog,我无法使我的绑定在ngDialog模式和我的控制器之间工作.我通过指定{scope:$scope}将控制器的范围注入模态,并且我可以访问控制器中定义的方法,但是在控制器中定义的模型的绑定不能正常工作.
这里是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: [] };
这是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>
这里是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>
模态可以访问父范围;它成功调用$scope.saveAddress()和console.log($scope.organization)来记录整个组织,包括在模态中添加的新地址.但是,新的地址不会反映在organization.html中的ng重复中,如果我添加了多个地址,则只会在日志中显示最近的地址.
$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); };
并更改了organization.html中的“添加地址”按钮以匹配:
<button class="button color-c vertical-a float-right" ng-click="pushAddress()">Add an Address</button>
解决方法
不要将controller参数传递给ngDialog.open.当您这样做时,将为对话框的实例创建一个新的控制器,这就是为什么您看到它“工作”.它正在访问该另一个控制器实例的范围和变量,而不是您传入的$scope中的范围和变量.
所以只需将你打开的对话框代码改成这个就可以了:
$scope.addAddress = function(){ ngDialog.open({ template: 'partials/modals/add-address.html',scope: $scope }); };
这是我创建的working plunker.