javascript - How to use Ionic Popup with scope in Controller As syntax? - Stack Overflow

I have an Angular controller setup using Controller as syntax and I need to create an Ionic Popup with

I have an Angular controller setup using Controller as syntax and I need to create an Ionic Popup with a bound data field from one the methods. What I can't figure out is how to set the scope of the popup for the data-binding. I found this example but it seems really messy to me to use both this and $scope. Is there a better way to do this or should I just go back to the $scope method?

My controller looks like this (with questions marked):

.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' }
            ]
        });
    }
})

And my template looks like:

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

Any thoughts would be appreciated. Thanks, Jason

I have an Angular controller setup using Controller as syntax and I need to create an Ionic Popup with a bound data field from one the methods. What I can't figure out is how to set the scope of the popup for the data-binding. I found this example but it seems really messy to me to use both this and $scope. Is there a better way to do this or should I just go back to the $scope method?

My controller looks like this (with questions marked):

.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' }
            ]
        });
    }
})

And my template looks like:

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

Any thoughts would be appreciated. Thanks, Jason

Share Improve this question asked Jun 11, 2015 at 21:37 JasonJason 2,6174 gold badges43 silver badges48 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

In that example they inject $scope so they can assign it as the scope property in the popup show options.

Maybe if I clear up what the "controller as" syntax is actually doing, you won't feel like it's so messy :)

When you do ng-controller="myController as ctrl", all you're doing is declaring a variable called ctrl on the $scope for that controller and setting it's value to the this for that controller. Doing "myController as ctrl" is literally exactly the same as doing it without the "as" shortcut like this:

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

Then in your controller:

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

The only difference when you do "myController as ctrl" is that $scope.ctrl = this just happens for you behind the scenes.

Check out this demo for proof that they are one and the same.

What they are doing in the demo you linked appears to be totally fine in my opinion ;)

Here's a blog post I wrote about "controller as" if you're interested: http://codetunnel.io/angularjs-controller-as-or-scope/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745102085a4611334.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信