1

How can you display a user's input in another html file in AngularJS?

<div ng-app>
  <div>
    <label>Input:</label>
    <input type="text" ng-model="uInput" placeholder="Enter text here">
    <hr>
  </div>
</div>

another file:

<div>
   <h1>{{ uInput }}</h1>
</div>

Right now, it takes the input but doesn't display it in the new page.

In other words, how do I pass the user input to the other view?

4
  • 1
    And how are you sending the user-input to the other file? Commented Jun 27, 2015 at 21:23
  • Use the same controller for both the files. Commented Jun 27, 2015 at 21:26
  • How would you do that? @NoCanDo Commented Jun 27, 2015 at 21:27
  • 1
    First simple way, Declare a controller like, ng-controller="someController". Declare these on body or div or whatever you use on those two pages. So essentially both the pages would be using same controller and shall have same values on scope. Second way, you can have routes setup, there you can mention template url and controllers. Here you shall have template urls as "page1.html" and "page2.html" but the controller declaration can be same as "someController". Commented Jun 27, 2015 at 21:39

2 Answers 2

1

Simplest way, have the same controller for both the pages,

Page1.html

<div ng-app="app">
  <div ng-controller="someController">
    <label>Input:</label>
    <input type="text" ng-model="uInput" placeholder="Enter text here">
    <hr>
  </div>
</div>

Page2.html

<div ng-app="app" ng-controller="someController">
   <h1>{{ uInput }}</h1>
</div>

And your controller could be,

app.controller('someController', function($scope){
    //your controller code, bind variables here
    //in this case, just the declaration will do
});

Alternatively, you can use Service injection, routing etc.

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

Comments

0

I assume that you are familiar with routes and services.

Use services, first load

$scope.first_controller= uInput;

then use services.

myservice.variablename = $scope.first_controller;

then:

$scope.second_controller= myservice.variablename;

and you shoud display in other view like this:

{{  second_controller }}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.