gpt4 book ai didi

javascript - 如何使在 ngDialog 模式中所做的更改出现在主页上

转载 作者:可可西里 更新时间:2023-11-01 02:31:27 25 4
gpt4 key购买 nike

我是 AngularJs 和 ngDialog 的新手,我无法让我的绑定(bind)在 ngDialog 模式和我的 Controller 之间工作。我通过指定 { scope: $scope } 将 Controller 的作用域注入(inject)到模态中,并且我可以访问 Controller 中定义的方法,但是与 Controller 中定义的模型的绑定(bind)无法正常运行。

我正在尝试使用模式来允许用户向组织添加地址。

这是 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) 记录了整个组织,包括在模态中添加的新地址。但是,新地址并没有反射(reflect)在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 中,并且每个控制台日志都包含所有地址,而不仅仅是最近的地址。

这两种方法有什么区别?为什么在使用 Controller 范围内的方法进行模态更改时,它们会“过期”?

最佳答案

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

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

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

这是一个working plunker我创造的。

关于javascript - 如何使在 ngDialog 模式中所做的更改出现在主页上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23996940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com