Unspecified variable in AngularJS data binding with Onsen UI

I am new to Onsen UI and AngularJS, and I have a simple question about data binding. When I use the variable $scope.name with ng-model in an Onsen UI template, it returns as UNDEFINED. Here is my code:

<!doctype html>
<html lang="en" ng-app="simpleKamus">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="lib/onsen/css/onsenui.css"/>
    <link rel="stylesheet" href="lib/onsen/css/onsen-css-components.css"/>
     <script src="lib/onsen/js/angular/angular.js"></script>
    <script src="lib/onsen/js/angular/angular-route.js"></script>
    <script src="lib/onsen/js/onsenui.js"></script>
    <script>
               var nameApp=angular.module('simpleKamus',['onsen']);
               nameApp.controller('KamusCtrl',['$scope','$http',function ($scope,$http){
                    $scope.tes=function(){
                       alert($scope.name);
                    };

                 }]);

    </script>
  </head>
  <body ng-controller="KamusCtrl">
<ons-tabbar>
    <ons-tab page="english.html" label="English - Indonesia" icon="ion-chatbox-working" active="true"></ons-tab>
        <ons-tab page="indo.html" label="Indonesia - English" icon="ion-chatbox-working"></ons-tab>

</ons-tabbar>

<ons-template id="english.html">
   <ons-toolbar>
    <div class="center">ENGLISH - INDONESIA</div>
  </ons-toolbar>

    <input type="text" ng-model="name" class="text-input text-input--transparent" placeholder="name" style="width: 100%">

    <button ng-click="tes()">tes</button>

    </ons-page>
</ons-template>
  </body>
</html>

When I click the "tes" button, why does $scope.name return as UNDEFINED? If I remove all of Onsen's tags and only leave the HTML tag, the variable $scope.name is defined. I have tried the solutions provided in this link, but they did not solve the problem. Thank you for your help.

Answer №1

You forgot to include the <ons-page> tag in your code. This is causing the controller not to be recognized after the ons-template. Moving it within the ons-page element should solve the issue. I have made the necessary modifications to your code:

<!doctype html>
<html lang="en" ng-app="simpleKamus">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="lib/onsen/css/onsenui.css"/>
    <link rel="stylesheet" href="lib/onsen/css/onsen-css-components.css"/>
     <script src="lib/onsen/js/angular/angular.js"></script>
    <script src="lib/onsen/js/angular/angular-route.js"></script>
    <script src="lib/onsen/js/onsenui.js"></script>
    <script>
               var nameApp=angular.module('simpleKamus',['onsen']);
               nameApp.controller('KamusCtrl',['$scope','$http',function ($scope,$http){
                    $scope.tes=function(){
                       alert($scope.name);
                    };

                 }]);

    </script>
  </head>
  <body>
    <ons-tabbar>
        <ons-tab page="english.html" label="English - Indonesia" icon="ion-chatbox-working" active="true"></ons-tab>
        <ons-tab page="indo.html" label="Indonesia - English" icon="ion-chatbox-working"></ons-tab>
    </ons-tabbar>

    <ons-template id="english.html">
        <ons-page  ng-controller="KamusCtrl">
            <ons-toolbar>
                <div class="center">ENGLISH - INDONESIA</div>
            </ons-toolbar>
        
            <input type="text" ng-model="name" class="text-input text-input--transparent" placeholder="name" style="width: 100%">
        
            <button ng-click="tes()">tes</button>
    
        </ons-page>
    </ons-template>
  </body>
</html>

p.s. I recommend keeping the index page with the standard template and creating a separate english.html page for the content, including the controller.

Answer №2

When trying it for the first time, using ng-model="$parent.name" seems to be effective.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enhance your website with the jQuery autocomplete feature, complete with

Is there a way to incorporate smaller text descriptions alongside the search results displayed on my website? The descriptions are available in the data array used by autocomplete and can be accessed using the .result function by calling item.description. ...

Utilize the power of Elasticsearch.js along with Bluebird for enhanced performance

Recently delving into node.js, I've been exploring the powerful capabilities of the bluebird promise framework. One of my current challenges involves integrating it with the elasticsearch javascript driver. After some experimentation, I was able to su ...

Selenium WebDriver is struggling to locate an element using link text

I'm currently facing a challenge in selecting an element that contains an anchor within a paragraph inside a div. The HTML structure I am working with is as follows: <a class="item" ng-href="#/catalog/90d9650a36988e5d0136988f03ab000f/category/DATA ...

Auth0 and Vue.js work together seamlessly to easily redirect users back to their previous page

I have integrated auth0 into my Vue.js application for handling logins. Within my AuthService, I manage all the auth0 operations. In the constructor, I instantiate a new auth0 object and set the default redirectUrl to the desired location: import auth0 f ...

JavaScript shortening a string while joining it

I'm facing a challenge with string truncation in my laravel and JS(jquery) app. Initially, I suspected it was an issue with the backend (as indicated in my question here: Laravel Truncating Strings). However, after thorough debugging, I discovered tha ...

Having trouble configuring individual route files in Express JS

I'm having trouble separating my login route from the default app.js and route/index.js files. When I try to access localhost:3000/login, I keep getting a 404 Not Found error. I've searched on StackOverflow for similar questions and tried follow ...

Tips for repositioning the window when linking to a different section of a website

As a beginner in HTML and CSS, I am currently working on adding a navigation bar to my one-page website. I want the navigation bar to smoothly guide users to different sections of the page when they click on a link. To achieve this, I am using anchors and ...

Using jQuery's toggleClass method to implement CSS transformations

I have a question about this situation Each time the button is clicked, jQuery toggles the class: .transition { background-color: #CBA; transform: translateX(100px) scale(0.5) rotate(180deg); } Within the <div class="container2"> And upon ...

The input box within the form is insufficiently sized

My task is to generate textareas with custom parameters like "cols" and "rows" and display them in forms. However, these parameters seem not to be functioning as expected. Whether I set cols=1000 or cols=1, there is no difference in the output. After revie ...

Strange occurrences within the realm of javascript animations

The slider works smoothly up until slide 20 and then it suddenly starts cycling through all the slides again before landing on the correct one. Any insights into why this is happening would be greatly appreciated. This issue is occurring with the wp-coda- ...

Is it possible to exclusively focus on the specified data attributes?

Is there a method to specifically target the 'data-season' attribute with the value "winter"? This is my HTML: <li data-season="summer">summer <ul class="groups"> <li data-item="beach">beach</li> <li data-item ...

Extract the Top X elements from a multidimensional array

Consider an Array that consists of nested arrays: [ ["2000-01-01", "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d1a9a8abe091b6bcb0b8bdffb2bebc">[email protected]</a>", 1, 9, 338], ["2000-01-01", "<a href="/ ...

Error 600: The element you have selected is not a valid target for this action. (Internet Explorer exclusive

I have been developing an AJAX calendar that functions perfectly in Chrome, Safari, and Firefox. However, I am encountering issues with Internet Explorer 9 and earlier versions. The error message I am receiving is SCRIPT 600: Invalid target element for th ...

What techniques can I employ in HTML and CSS to design the user interface for my Java application?

Recently, I came across an interesting article at this link: http://docs.oracle.com/javafx/2/webview/jfxpub-webview.htm. The article demonstrates how to develop a java application utilizing the javafx library and utilizing classes like WebEngine and WebVie ...

What is the best way to connect a nested Vuex object to a Vue.js property within the Tiptap text editor?

I'm attempting to connect the nested Vuex state object to the editor property of the tiptap's editor-content component. The structure of the state is as follows: <template> <table :style="{ backgroundColor: element.options.ba ...

Tips for generating a dynamic Array name to be sorted with React JS

My lack of experience is causing some issues for me. I am currently working on a form in react where the user has to select two values first. Based on these two values, a third value will be available for selection. However, the options for this third val ...

Trouble with sending arguments to Express middleware

I'm currently working on developing a custom input validation middleware using Express. My aim is to create a middleware that takes 2 parameters in order to validate client input effectively. Despite referencing various sources, including the Express ...

What is the best way to toggle the visibility of multiple column groups in Ag-Grid community using dynamic

I am seeking to replicate a basic version of the sidebar found in Ag-Grid Enterprise. The goal is to use JavaScript to gather all column groups within a grid and then provide a checkbox for each group to toggle visibility. While I am aware that individual ...

Incorrect configuration file for karma

To simplify the configuration of the karma config file, I have utilized variables to store specific parts of the path. var publicfolder = "public" var mypath = "packages/vendor/package/"; files: [ publicfolder+'/assets/libs/jquery/dist/jquery.js&a ...

Creating dynamic forms in Vue using v-for

I'm currently experimenting with creating dynamic form fields using v-for and vuex. My approach involves nesting a v-for inside another v-for. The process of adding new form fields works smoothly, but I encountered an issue when attempting to delete t ...