简体   繁体   English

如何在angularjs中组合两个控制器

[英]How to combine two controllers in angularjs

How to combine these two controllers into single controller class (make the two objects of the single controller class) and how use the same in html 如何将这两个控制器组合为单个控制器类(使单个控制器类的两个对象)以及如何在html中使用相同的控件

var firstController = function ($scope)
{
  // Initialize the model variables
  $scope.firstName = "John";
  $scope.middleName = "Doe"
  $scope.lastName = "Smith";

  // Define utility functions
  $scope.getFullName = function ()
  {
    return $scope.firstName + " " + $scope.lastName;
  };
};

var secondController = function ($scope)
{
  // Initialize the model variables
  $scope.firstName = "Bob";
  $scope.middleName = "Al";
  $scope.lastName = "Smith";

  // Define utility functions
  $scope.getFullName = function ()
  {
    return $scope.firstName + " " + $scope.middleName + " " + $scope.lastName;
  };
};

You can create a component 您可以创建一个组件

angular.module('plunker').component('person',
{
  templateUrl: 'person.html',
  bindings: {
      firstName: '<',
      middleName: '<',
      lastName: '<'
    }
});

here is a working plunkr : 这是一个工作的朋克:

https://plnkr.co/edit/XJSk0ihDTX3pgsab81mO https://plnkr.co/edit/XJSk0ihDTX3pgsab81mO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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