栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

带有oop继承的angularjs

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

带有oop继承的angularjs

您的猜测听起来完全适用。

You can reuse functionality defined in parent controllers by simply calling
methods attached to the parent scope:

HTML

<div ng-controller="ParentCtrl">    <!-- Something here ... -->    <div ng-controller="ChildCtrl">        <!-- Something here ... -->    </div>    <!-- Something here ... --></div>

Javascript

function ParentCtrl($scope) {    $scope.parentMethod = function () {        //method body    };}function ChildCtrl($scope) {    $scope.childMethod = function () {        //functionality        $scope.parentMethod();        //functionality    };}

If you want to use the Javascript approach with prototype inheritance you can
use:

var myApp = angular.module('myApp',[]);function Parent($scope) {    $scope.name = 'Superhero';    $scope.clickParent = function() {        $scope.name = 'Clicked from base controller';    }    }function Child($scope, $injector) {    debugger;    $injector.invoke(Parent, this, {$scope: $scope});    $scope.name = 'Superhero Child';    $scope.clickChild = function(){        $scope.clickParent();    }       }Child.prototype = Object.create(Parent.prototype);

http://jsfiddle.net/mhevery/u6s88/12/

For services, for example, you can use:

(function () {function ParentService(arg1) {   this.arg1 = arg1;}function ChildService(arg1, arg2) {   ParentService.call(this, arg1);   this.arg2 = arg2;}ChildService.prototype = new ParentService();app.service('ChildService', ChildService);}());

Also check
this
discussion and the blog post about inheritance in
AngularJS I posted.



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/633254.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号