简体   繁体   中英

How to call function from module and show record in view file using MEAN stack?

I am working in MEAN stack and I using the controller and module file.

module.js

var app = angular.module("myApp", ['ngRoute', 'ngStorage']);
app.config(function($routeProvider) {
  $routeProvider.
  when('/allusers', {
    controller: 'usercontroller',
    templateUrl: 'js/view/allusers.html'

  });
});

Now I want to call a usercontroller function. How can I call function which is defined into the controller?

My controller.js file is

app.controller("usercontroller", function($scope, $http, $localStorage, $location) {

  $scope.allusers = function() {

    $http({
      method: 'POST',
      url: '/api/addseat',
      data: { email: 'shahjad.ahmad89@gmail.com', password: 123456 }
    }).then(function successCallback(response) {
      if (response.data.error) {
        alert("Invalid email pasword");
      } else {
        $scope.dp = response.data;
        $localStorage.pp = $scope.dp;
      }
    }, function errorCallback(response) {
      alert("Invalid email pasword");
    });
  }
});

How to call the allusers function from the module and how to display the record into view file?

You can call the allusers function inside your controller by default when your controller loads as

$scope.allusers();

You can also call it through any button click or other event using ng-click attribute.

<input type="button" value="Submit" ng-click="allusers()"/>

And display the value as,

For single value, <div>{{dp}}</div>

For multiple values use ng-repeat ,

<div ng-repeat="d in dp">{{d}}</div>

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM