angularjs - Controller As inheritance with $controller -
i have pretty big question issue thought might easy split in smaller ones.
so, i'm trying use $controller inherit parentcontroller, things not working expected.
i have jsfiddle here. , code.
angular .module("app",[]) .controller('parentcontroller', parentcontroller) .controller('childcontroller', childcontroller) parentcontroller.$inject = ['$scope'] childcontroller.$inject = ['$controller', '$scope']; function parentcontroller($scope) { var vm = this; vm.foo = 'parent foo'; } function childcontroller($controller, $scope) { $controller('parentcontroller vm', {$scope: $scope}); var vm = $scope.vm; // view model }
and simple html
<div> {{vm.foo}} </div>
when configure app use parentcontroller, this
<body ng-app="app" ng-controller="parentcontroller vm">
it works fine.
but now, childcontroller, not working, it's vm not include property foo created in parentcontroller.
what doing wrong?
many thanks.
if want go approach need extend child controller base parent controller. can use angular.extend this:
function childcontroller($controller, $scope) { angular.extend(this, $controller('parentcontroller', {$scope: $scope})); this.bar = 'child bar'; }
just make sure first extend child controller instance object (this
) parent controller instance, , after define new child properties.
Comments
Post a Comment