10

我是 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: []
    };

这是组织.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)记录了整个组织,包括模态中添加的新地址。但是,新地址并没有反映在ng-repeatorganization.html中,如果我一个接一个地添加多个地址,日志中只会显示最近的一个。

作为一个实验,我在 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);
};

并更改了 organization.html 中的“添加地址”按钮以匹配:

<button class="button color-c vertical-a float-right" ng-click="pushAddress()">Add an Address</button>

现在,当我单击“添加地址”时,新地址会立即显示在 中ng-repeat,并且每个控制台日志都包含所有地址,而不仅仅是最近的地址。

这两种方法有什么区别?为什么在使用控制器范围内的方法进行模式更改时会“过期”?

4

1 回答 1

24

不要将controller参数传递给ngDialog.open. 当您这样做时,将为该对话框实例创建一个新控制器,这就是您看到它“工作”的原因。它访问的是另一个控制器实例的范围和变量,而不是$scope您传入的那个。

因此,只需将您打开的对话框代码更改为此,它将起作用:

$scope.addAddress = function(){
    ngDialog.open({
        template: 'partials/modals/add-address.html',
        scope: $scope
    });
};

这是我创建的一个工作 plunker 。

于 2014-06-02T19:14:29.780 回答