1

I am building a cross platform app using AngularJS, Monaca and Onsen UI.

I am trying to call a function in my controller from a ng-click() on my view. However, when I click the button that calls the function I get an error message saying: "Uncaught ReferenceError: checkUserLoginDetails is not defined"

My index.html looks as follows

<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
    <!-- Usual scripts goes here -->
    <script src="js/app.js"></script>
    <script src="js/login.js"></script>
    <script src="js/sharedProperties.js"></script>
</head>

<body>
    <ons-navigator var="myNavigator" page="login.html"></ons-navigator>
</body>
</html>

I cant see where I am going wrong here.

My This is my login.html where I call my function.

<div ng-controller="LoginController">
    <ons-page>
        <form class="login-form" style="text-align: center" name="myForm">
            <section style="padding: 8px">
                <input type="password" 
                    class="text-input--underbar" 
                    required 
                    minlength="3" 
                    maxlength="4" 
                    ng-model-options="{ debounce : 500 }"
                    placeholder="User ID" 
                    ng-model="userID" >
            </section>


            // Button not working here
            <section style="padding: 0 8px 8px">
                <ons-button var="saveBtn" ng-disabled="myForm.$invalid" modifier="large" ng-click="checkUserLoginDetails()">Log In</ons-button>
            </section>
        </form>
    </ons-page>
</div>

And this is my loginController.js code that handles the logic (logic omitted)

var login = angular.module("loginController", []);
login.controller("LoginController", function($scope, $http, SharedProperties)
{   
    // Watch for changes in the User ID text field
    $scope.$watch('userID', function(newVal, oldVal)
    {
       // Code omitted but WORKING
    });

    // Check user Logins
    $scope.checkUserLoginDetails = function()
    {
        console.log("Getting here..."); // Not getting here
    }
});

And in my main app.js I create the module that holds all the controllers - again this is working for all other views and controller (again omitted from the list as not needed) so I doubt the issue is here.

var app = angular.module("myApp", ['onsen', 'sharedProperties', 'loginController']);

I have also tried onclick instead of ng-click but bot give the same error message.

11
  • Are you sure that your section is under the loginController? Commented May 18, 2016 at 9:55
  • Yes I am sure. I also have other functions on that controller to e.g. $watch() for changes in text fields and they are all working as expected. My view is setup as <div ng-controller="LoginController"> <ons-page><section>...my section here</section></ons-page></div> Commented May 18, 2016 at 9:58
  • 2
    you need to see this github.com/OnsenUI/OnsenUI/issues/395. Commented May 18, 2016 at 10:02
  • 2
    we need more code to see what is wrong there because there is no reason for ng-click to not work and therefore I'd like to see the html section and app section. Please check if all things are initialized properly. Commented May 18, 2016 at 10:03
  • 1
    can you try with normal button instead of ons-button. I just wanted to know. Commented May 18, 2016 at 10:32

1 Answer 1

1

If ons-page is a directive with transclution, then the function isn't available in the transclution body since directives with transclude create their own scopes.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you suggest a possible solution in this case please?
you can try to move the logic from the loginController into the ons-page directive

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.