gpt4 book ai didi

javascript - 如何在 Controller As 语法中使用 Ionic Popup with scope?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:33:26 26 4
gpt4 key购买 nike

我有一个使用 Controller 作为语法的 Angular Controller 设置,我需要从其中一个方法创建一个带有绑定(bind)数据字段的 Ionic Popup。我想不通的是如何为数据绑定(bind)设置弹出窗口的范围。我找到了 this example 但同时使用 this 和 $scope 对我来说似乎真的很困惑。有没有更好的方法来做到这一点,还是我应该回到 $scope 方法?

我的 Controller 看起来像这样(标记了问题):

.controller('AccountCtrl', function ($ionicPopup) {
// Properties ===========
this.resetData = {};

// Methods ==============
this.forgotPassword = function () {
var myPopup = $ionicPopup.show({
template: '<input type="text" ng-model="<What goes here?>.resetData.resetEmail">',
title: 'Please enter your e-mail address',
scope: <What goes here?>
buttons: [
{
text: 'Submit',
onTap: function (e) {
console.log("Password reset:" + <What goes here?>.resetData);
}
},
{ text: 'Cancel' }
]
});
}
})

我的模板看起来像:

<ion-view view-title="Sign In">
<ion-content has-bouncing="false" ng-controller="AccountCtrl as account">
<a href="#" ng-click="account.forgotPassword()">Forgot your password?</a>
</ion-content>
</ion-view>

如有任何想法,我们将不胜感激。谢谢,杰森

最佳答案

在该示例中,他们注入(inject)了 $scope,因此他们可以将其分配为弹出式 show 选项中的 scope 属性。

也许如果我弄清楚“controller as”语法的实际作用,您就不会觉得它太乱了:)

当你执行 ng-controller="myController as ctrl" 时,你所做的就是在 $scope 上声明一个名为 ctrl 的变量code> 并将其值设置为该 Controller 的 this 。执行“myController as ctrl”与不使用“as”快捷方式的操作完全相同:

<!-- markup -->
<div ng-controller="myController"></div>

然后在你的 Controller 中:

// controller
app.controller('myController', function ($scope) {
$scope.ctrl = this;
});

当您执行“myController as ctrl”时的唯一区别是 $scope.ctrl = this 只是在幕后发生。

查看 this demo以证明它们是一回事。

在我看来,他们在您链接的演示中所做的似乎完全没问题;)

如果您有兴趣,这是我写的一篇关于“ Controller 作为”的博文:http://codetunnel.io/angularjs-controller-as-or-scope/

关于javascript - 如何在 Controller As 语法中使用 Ionic Popup with scope?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30791900/

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