2017-07-11 48 views
0

兩個控制器結合如何將這兩個控制器結合成單一控制器類(使單個控制器類的兩個對象),以及如何使用相同的HTML如何angularjs

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

你爲什麼首先這樣做? – Alburkerk

+0

實施服務/工廠。 – Hoyen

回答

0

您可以創建一個組件

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

這裏是一個工作plunkr:

https://plnkr.co/edit/XJSk0ihDTX3pgsab81mO