triggering the onsen's setMainPage function when clicked using a variable from ng-repeat loop

I need help customizing my Onsen UI splitView app to dynamically set the main page based on a variable received from a clicked item in a list generated by ng-repeat.

After researching ng-repeat and ng-click, I am still unable to achieve the desired outcome. Perhaps I am not understanding them correctly.

Below is the relevant code:

HTML

<ons-split-view 
 var="splitView"
 secondary-page="secondary.html" 
 main-page="main1.html" 
 main-page-width="60%" >
</ons-split-view>

<ons-template id="main1.html" >
    <ons-page ng-controller="ApkController as page">
            <ons-toolbar>
                <div class="center">List of pages</div>
            </ons-toolbar>
            <ons-list>
                <ons-list-item modifier="chevron" ng-repeat="content in page.contents" onclick="splitView.setMainPage(content.name'.html');">{{content.number}}. {{content.name}}</ons-list-item>
            </ons-list>
    </ons-page>
 </ons-template>

<ons-template id="secondary.html">
    <ons-page style="border-right: 1px solid #ddd;text-align:center; vertical-align:middle;">
        <ons-toolbar>
            <div class="left"><span class="toolbar-button--quiet navigation-bar__line-height" ><i class="fa fa-angle-left fa-lg" style="color: #666"></i> Back</span></div>
        </ons-toolbar>
        <table style="width:100%;height:100%;">
            <tr >
                <td style="vertical-align:middle;text-align:center">Please choose the page you want </td>
            </tr>
        </table>
    </ons-page>
 </ons-template>

<ons-template id="page1.html">
  <ons-page>
    <ons-toolbar>
      <div class="center"> Page 1 </div>
    </ons-toolbar>
    <ons-button onclick="splitView.setMainPage('main1.html')">Click</ons-button>
  </ons-page>
</ons-template>

<ons-template id="page2.html">
  <ons-page>
    <ons-toolbar>
      <div class="center"> Page 2 </div>
    </ons-toolbar>
    <ons-button onclick="splitView.setMainPage('main1.html');">Click</ons-button>
  </ons-page>
</ons-template>

<ons-template id="page3.html">
  <ons-page>
    <ons-toolbar>
      <div class="center"> Page 3 </div>
    </ons-toolbar>
    <ons-button onclick="splitView.setMainPage('main1.html')">Click</ons-button>
  </ons-page>
</ons-template>

Javascript

ons.bootstrap().controller('ApkController', function () {
    this.contents = something;
});

var something = [
{
    number : '78',
    name : 'page1',            
},
{
    number : '79',
    name : 'page2',
},
{
    number : '80',
    name : 'page3',
}];

You can also view the code on CodePen: http://codepen.io/fadynoor/pen/GJJMLx

The issue lies in inserting content.name into the setMainPage() function so it functions as intended.

I am new to Onsen UI and Angular, any suggestions would be greatly appreciated.

P.S.: This is my first time posting in this forum, apologies for any mistakes made.

Answer №1

If you implement the following adjustments, your code will be functional

<ons-list-item modifier="chevron" ng-repeat="content in page.contents" ng-click="showContent( content)">{{content.number}}. {{content.name}}</ons-list-item>

The JavaScript controller should resemble this

ons.bootstrap().controller('ApkController', function ($scope) {
  this.contents = something;

  $scope.showContent = function(content){
    splitView.setMainPage(content.name + ".html");

  };
});

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

Retrieving data from MySQL through AJAX does not yield any information

I have been following a tutorial from W3 schools on PHP and AJAX database. Majority of the content is working fine, however it seems that there is no data being retrieved from the MySQL database I created called "exercises" in the "exercisedb" database. B ...

What is the best way to dynamically select and deselect radio buttons based on a list of values provided by the controller?

I am currently working on implementing an edit functionality. When the user initially selects a list of radio buttons during the create process, I store them in a table as a CSV string. Now, during the edit functionality, I retrieve this list as a CSV stri ...

The functionality of the Protractor right click feature is smooth, however, there seems to be an issue with selecting

Even though I can locate the button within the context menu, I am facing difficulty in clicking it. The code mentioned below is successfully able to click the button, but an error message pops up indicating: Failed: script timeout (Session info: chr ...

Stop all animations in JS and CSS

Looking for a way to halt all CSS and JavaScript animations, including canvas and webGL effects, on a specific webpage. Some animations can cause slow performance on certain browsers like Opera and Firefox, so I'm seeking a code snippet or guidance o ...

I'm looking to center the column content vertically - any tips on how to do this using Bootstrap?

Hello! I am looking to vertically align the content of this column in the center. Here is an image of my form: https://i.stack.imgur.com/nzmdh.png Below is the corresponding code: <div class="row"> <div class="form-group col-lg-2"> ...

Having trouble shutting down Metro Bundler on Windows?

While working on my React Native development, I regularly use npm start to get things going. However, I've run into an issue recently when trying to stop the process using Ctrl + c. It seems like I can no longer use npm start smoothly: ERROR Metro ...

Encountering a problem with controlling the number of splits allowed

I am encountering an issue with splitting my string using the code below. splitter.map((item1) => { let splitter1 = item1.split("=")[0].trimLeft(); let splitter2 = item1.split("=")[1].trimRight(); }); The content of item1 is as fo ...

Improving the efficiency of your if else code in React js

I am looking for a way to optimize my code using a switch statement instead of multiple if-else conditions. How can I achieve this? Here is the current version of my code: handleChange = (selectedkey) => { this.setState({ activeKey: selectedkey } ...

Struggling to align my image in the center while applying a hover effect using CSS

Hey there, I'm having an issue centering my image when I add instructions for it to tilt on mouseover. If I take out the 'tilt pic' div, the image centers just fine. Can anyone help me identify what I might be doing wrong? Thanks in advance! ...

React application not functioning on localhost:3000 despite successful compilation with no errors, only displaying the title on localhost but failing to show any content

I recently started developing a new website with React. Everything was running smoothly on localhost until I made some changes, and now the homepage content is not displaying when I visit localhost:3000. I suspect there may be an issue with my routing or s ...

jQuery Datatables provide a powerful and customizable way to display

I've been working on a project that requires me to incorporate Grid functionality into the displayed data. To achieve this, I'm utilizing the Datatable plugin from jquery. The implementation is smooth and flawless. However, a concern arises wh ...

Lock the initial column in an HTML table

Hey there! I've been trying to freeze the first column of my HTML table, and while I managed to do so after a few attempts, I encountered an issue. When I scroll the table horizontally, the columns on the left seem to overlap with the first column, an ...

How can I ensure that the AppBar background color matches the color of the navigation bar?

Having trouble changing the background color of the <AppBar> in my project. Using the Container component to set a maximum screen size, but encountering issues when the screen exceeds this limit. The AppBar background color appears as expected while ...

The OpenWeatherMap API is displaying 'undefined' as a response

I've been encountering some issues with my weather app project due to lack of clarity in my previous questions. To be more specific, every time I attempt to retrieve weather information in JSON format using the fetch method, it keeps returning undefin ...

Get information that has been uploaded using ajax

I need help with my code for sending data via ajax to an update.php page. $(document).ready(function() { $("#modify").click(function() { var a = $("#a").val(); var b = $("#b").val(); var c = $("#c").val(); $.ajax({ type: "POST", ...

Having trouble retrieving values from JSON properties

My mind is boggled by this issue, and I have a feeling it's just a simple oversight! I'm dealing with a service that sends back a JSON object: [{"accountId":"0000004000006195","title":null,"firstName":"JOE","middleName":"BLOG","lastName":"BLOGG ...

Ways to implement a single AJAX function for multiple buttons

I need to call the same AJAX function for multiple buttons. Please assist with the code provided below. This particular code will create buttons and upon clicking on them, it displays details... please assist with resolving this issue. The code generated ...

Modifying the default error message in Yup: A step-by-step guide

What is the process for modifying the default error message to a custom error message? Specifically, my custom message. const VALIDATION_SCHEME = Yup.object().shape({ numOne: Yup.Number().required('!'), numTwo: Yup.Number() .r ...

Enable AJAX to dynamically load pages without caching

I am currently using an ajax function to refresh a specific division on a webpage with HTML content. <script> $('#scene_container').load('scene.html', function () { cache: false }); </script> ...

Angular: Clicking on a component triggers the reinitialization of all instances of that particular component

Imagine a page filled with project cards, each equipped with a favorite button. Clicking the button will mark the project as a favorite and change the icon accordingly. The issue arises when clicking on the favorite button causes all project cards to rese ...