AngularJs -在js指令中链接js代码
AngularJs - Linking js code within js directive
对于我正在构建的angularjs应用程序,我有一个非常详细的指令。我想打破代码分离JS文件,然后将它们包含到1 JS指令文件。例如,与其将所有代码放在一个js文件中,不如这样写:
myapp-directive-http-request.js
myapp-directive-init.js
myapp-directive-event.js
myapp-directive-function.js
然后myapp-directive.js会链接到上面所有的文件。
几乎我正在寻找一个类似的函数包括("FILEPATH")在PHP或作为ng-include在angularjs html视图。有可能在指令部分内做这个javascript或angularjs吗?
我的指令代码如下:
angular.module("myApp", [])
.directive("myApp",['$http', function($http){
return{
link: function(scope,element,attrs){ // normal variables rather than actual $scope, that is the scope data is passed into scope
//all functions, init etc code is here
},//return
restrict:"A", //assign as attribute only ie <div my-modal> Content </div>
replace:true,//replaces div with element, note if this is the case must all template must be wrapped within one root element. eg button is within ul otherwise get an error.
templateUrl:"partials/myapp/myapp.html",//template
transclude:true, //incorporate additional data within
scope:{
} //If on this should mean the html input is not binded to custom directive
}//return
}])
使用您选择的任何构建系统。Webpack、RequireJS、Browserify、JSPM——它们都能做到这一点。require('...')
函数(在JSPM的情况下是ES2015-fashioned System.import
)将完成这项工作。
myapp-directive-http-request.js
myapp-directive-init.js
myapp-directive-event.js
myapp-directive-function.js
看起来不像是一个好的分手。臃肿的指令或控制器通常是糟糕设计的标志。一般来说,你可能想要把所有与DOM或作用域无关的东西分开,并保持指令的完整。从一个指令生成多个指令也可能是有益的。
相关文章:
- Javascript-点击时阻止JS链接滚动回顶部
- 使用jquery.chained.min.js链接选择不起作用
- 树布局中的d3.js链接分离
- 如何使这个JS链接工作
- 用q.js链接承诺
- GET 404 (未找到) - 角度 js 链接路由
- 节点.js链接多个承诺(与猫鼬)
- 好奇!js 链接在文件末尾不起作用
- 如何将记录与 lawnchair js 链接
- 如何正确地将条件(?)承诺与 Q.js 链接
- 访问了 chrome 中的 JS 链接
- 没有协议的CSS和JS链接
- Google Apps Script 具有多个样式表和 js 链接
- 如何使用three.js链接两个几何对象
- PHP在index.PHP中包含CSS和JS链接的文件
- 使用 Greasemonkey 更改变量或激活 JS 链接的<选择>下拉菜单
- 用Office-JS链接读写
- 为什么这个JS链接需要2次点击才能触发?
- 如何在c#中使用Selenium点击JS链接
- Ember js链接模板包括