外挂式插件编程思路【进阶篇】(6)

野生程序猿-杂烧3年前案例分享842

深入模拟低代码编程


【index.html】

<!DOCTYPE html>
<html ng-app="myApp"  ng-controller="myhtml">
	<head>
		<meta charset="utf-8" />
		<title ng-bind="title"></title>
	</head>
	<body>
		<script src="index.js"></script>
	</body>
</html>

【index.js】部分模拟程序直接生成的代码

//electron 加入下面一行
if(typeof module === 'object') {window.module = module; module = undefined;}
class load_js{
	js_list=[];
	active_index=0;
	
	back=null;
	constructor(js_list,back)
	{
		this.js_list=js_list;
		this.back=back;
		this.load_js_one();
	}
	load_js_one()
	{
		let me=this;
		var js = document.createElement("script");
		document.getElementsByTagName("head")[0].appendChild(js);
		js.src = this.js_list[this.active_index];
		js.onload = js.onreadystatechange = function()
		{
		    if (!this.readyState || 'loaded' === this.readyState || 'complete' === this.readyState) {
				console.log('成功',js.src);
				if(me.active_index==me.js_list.length-1)
				{
					
					me.back();
				}else{
					me.active_index=me.active_index+1;
					me.load_js_one();
				}
		    }
		}
	}
}
new load_js(['jquery.min.js','angular.min.js','app.js'],function()
{
	//debugger;
	console.log('end');
	console.log($);
	$("body").append(`

			<style>
				
				dt {
					border: 1px rgba(0,0,0,0.05) solid; border-bottom:0;
					list-style: none; margin: 0;
					background: rgba(0,0,0,0.02);
					border-radius: 8px 8px 0px 0px;
					overflow: hidden;
				}
				dd {
					border: 1px rgba(0,0,0,0.05) solid;  border-top:0;
					list-style: none; margin: 0;
					padding: 10px 10px 10px 10px;
					border-radius:0px 0px 8px 8px;
				}
				dt span { 
					background: rgba(0,0,0,0.03);
					display: inline-block; height: 40px; line-height: 40px; padding: 0px 20px 0px 20px;
					cursor: pointer;
					border-left: 1px rgba(0,0,0,0.05) solid;
					
				}
				dt span:nth-child(1) {border-left:0px;}
				dt span.active {
					background:#ff0000;
					color:#fff; font-weight: bold;
					
				}
			</style>
			<div id="tab_body">
				<h1>模拟tab切换</h1>
				<dl>
					<dt><span class="{{$index==tab.active_index ? 'active':''}}" ng-repeat="x in tab.list" ng-click="tab.change($index)">{{x.title}}</span></dt>
					<dd>{{tab.list[tab.active_index].content}}</dd>
				</dl>
			</div>
	`);

	//加载tab html

	
	var app=ng_app();
	app.init(function()
	{
		//tab标签相关
		app.$scope.tab={
			active_index:0,
			list:[
				{title:'首页',content:'首页内容'},
				{title:'新闻',content:'新闻列表'},
				{title:'产品',content:'产品列表'},
			],
			change:function(index){
				console.log('change',index);
				if(app.$scope.tab.active_index!=index) app.$scope.tab.active_index=index;
			}
		};
		app.$scope.title="外挂式插件编程思路(进阶)by野生程序猿-杂烧";

		app.update();//页面渲染
	});
});

【app.js】

function ng_app()
{
	var MyApp=
	{
		init:function(back)
		{
			console.log('init');
			var me=this;
			var app = angular.module('myApp',[]);
			app.controller("myhtml",function($scope, $http,$window,$compile)
			{
				me.$scope=$scope;
				me.$http=$http;
				me.$window=$window;
				me.$compile=$compile;
				back();
			});
		}
		
		//更新模板渲染 优化ng频繁更新有报错,这里加1毫秒延迟 避免报错
		,update:function(){
			var me=this;
			setTimeout(function(){
				try{
					me.$scope.$apply();
				}catch(e){}
			},1);
		},
		//配合jq使用,jq后的模板,重新渲染
		html:function(html)
		{
			var that=this;
			try{
				var result=that.$compile(html)(that.$scope);
				if(result.selector)//假设存在 this_tpl_new.selector 则认为ng变量不存在失败了
				{
					//console.error(this_tpl_new.selector);
					var result=html;
				}
			}catch(e){
				var result=html;
			}
			return result;
			
		},
		new:function(){
			return this;
		}
		
	}
	var result=MyApp.new();
	return result;
};

模拟完成,index.js里实现插入html,并支持angular.js的相关功能。

image.png

标签: 外挂式编程

相关文章

外挂式插件编程思路【敏捷框架篇】(9)

外挂式插件编程思路【敏捷框架篇】(9)

有了外挂式思维基础,开始涉及框架。个人理解,所谓框架就是自己研究一些习惯、一些风格,出于某个目的(如快速开发),而设计并开发出的一套生产工具。我这里对框架的需求主要是两点1、快。为了减少开发的工作时间...

外挂式插件编程思路【进阶篇】(8)

外挂式插件编程思路【进阶篇】(8)

路由功能,单页面应用更加得心应手。【index.html】<!DOCTYPE html> <html ng-app="myApp" ...

外挂式插件编程思路【敏捷框架篇】(10)

外挂式插件编程思路【敏捷框架篇】(10)

优化细节,标签多个效果。如select正常得到的是value值,但是我们实际要在其他地方显示value对应的文字。【index.html】<!DOCTYPE html> <...

外挂式插件编程思路【基础篇】(3)

外挂式插件编程思路【基础篇】(3)

* 本网页 讲解外挂插法,内插(外部的东西往内部插)。 * window.plugin01,window.plugin02模拟2个外部用户,内部的逻辑方法会随着外部更改而...

外挂式插件编程思路【敏捷框架篇】(13)

外挂式插件编程思路【敏捷框架篇】(13)

zhaoshao.alert支持zs-select,结合才是一个相对复杂的应用场景。【index.html】<!DOCTYPE html> <html ng-a...

外挂式插件编程思路【敏捷框架篇】(11)

外挂式插件编程思路【敏捷框架篇】(11)

修复bug,mvvm框架主要是善于数据,虚拟dom,与实际的dom比,监听能力差很多。这里有个bug,select下拉选项点开后,需要点其他地方关闭。如果jq,其他的点击事件很好监听。这里不用jq,用...