如何在ngDialog模式中进行更改显示在主页面上

时间:2014-06-02 14:29:41

标签: javascript angularjs ng-dialog

我是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-repeat中,如果我一个接一个地添加多个地址,则只会在日志中显示最新的地址。

作为实验,我将此函数添加到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中,每个控制台日志都包含所有地址,而不仅仅是最新的地址。

这两种方法有什么区别?当使用控制器范围内的方法制作时,为什么模态中的更改会“过期”?

1 个答案:

答案 0 :(得分:24)

不要将controller参数传递给ngDialog.open。当你这样做时,会为该对话框的实例创建一个新的Controller,这就是你看到它&#34; working&#34;的原因。它正在访问该其他控制器实例的范围和变量,而不是您传入的$scope中的那个。

所以只需将打开的对话框代码更改为此代码即可:

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

这是我创建的working plunker