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

How to use Ionic Popup with scope in Controller As syntax?

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

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

.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>

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

在那个例子中,他们注入了 $scope,这样他们就可以在弹出 show 选项中将其分配为 scope 属性。

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

当您执行 ng-controller="myController as ctrl" 时,您所做的只是在 $scope 上为该控制器声明一个名为 ctrl 的变量,并将其值设置为 this对于那个控制器。执行 "myController as ctrl" 与不使用 "as" 快捷方式完全相同:

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

然后在你的控制器中:

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

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

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

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

这是一篇博客 post 我写的关于 "controller as" 如果你感兴趣的话:http://codetunnel.io/angularjs-controller-as-or-scope/