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

时间:2017-07-11 12:45:20

标签: angularjs angularjs-controller

如何将这两个控制器组合成单个控制器类(使单个控制器类的两个对象)以及如何在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;
  };
};

1 个答案:

答案 0 :(得分:0)

您可以创建一个组件

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

这是一个有效的插件:

https://plnkr.co/edit/XJSk0ihDTX3pgsab81mO