Tag: 指令

Angularjs中的指令模板函数有什么好处?

根据文档, template可以是一个函数,它接受两个参数,一个element和attributes并返回一个表示模板的string值。 它用HTML的内容replace当前元素。 replace过程将所有属性和类从旧元素迁移到新元素。 compile函数处理转换模板DOM。 它有三个参数,一个element , attributes和transclude函数。 transclude参数已被弃用。 它返回一个link函数。 看起来template和compilefunction非常相似,可以达到同样的效果。 template函数定义了一个模板, compile函数修改了模板DOM。 但是,它可以在template函数本身中完成。 我看不到为什么修改templatefunction以外的template DOM。 反之亦然,如果可以在compile函数中修改DOM,那么template函数需要什么?

AngularJS – 属性指令input值的变化

我有一个AngularJS的属性指令,我想随时采取行动,其父input的价值改变。 现在我正在使用jQuery: angular.module("myDirective", []) .directive("myDirective", function() { return { restrict: "A", scope: { myDirective: "=myDirective" }, link: function(scope, element, attrs) { element.keypress(function() { // do stuff }); } }; }); 有没有办法做到这一点,没有jQuery? 我发现keyPress事件并没有完全按照我想要的方式进行,虽然我确信我会提出一个解决scheme,但是当我在Angular项目中使用jQuery时,我有点紧张。 那么angular度怎么做呢?

如何检查一个指令的方法参数是否在AngularJS中指定?

我创build了一个包含一个button的自定义指令。 该button从“callback”属性指定的父范围中调用一个方法。 <!DOCTYPE html> <html ng-app="app"> <head> <title>Simple directive</title> <script src="js/lib/angular/angular.js"></script> <script type="text/javascript"> var app = angular.module('app', []); app.controller('TestController', function($scope) { $scope.doSomething = function(param) { alert('Something called with: ' + param); } }) app.directive('myDirective', function() { var ret = { restrict: 'E', scope: { user: '@', callback: '&' // bound a function from the scope […]

使用TypeScript和$ inject机制定义AngularJS指令

最近我开始重新构build我正在使用TypeScript的Angular项目之一。 使用TypeScript类来定义控制器非常方便,并且由于static $inject Array<string>属性,所以可以很好地处理缩小的JavaScript文件。 而且你可以得到相当干净的代码,不需要从类定义中分解angular依赖关系: module app { 'use strict'; export class AppCtrl { static $inject: Array < string > = ['$scope']; constructor(private $scope) { … } } angular.module('myApp', []) .controller('AppCtrl', AppCtrl); } 现在我正在寻找解决scheme来处理指令定义的类似情况。 我发现一个很好的做法来定义指令作为函数: module directives { export function myDirective(toaster): ng.IDirective { return { restrict: 'A', require: ['ngModel'], templateUrl: 'myDirective.html', replace: true, link: (scope: […]

C代码循环性能

我的应用程序中有一个乘加内核,我想提高它的性能。 我使用Intel Core i7-960(3.2 GHz时钟),并已经使用SSE内部函数手动实现了内核,如下所示: for(int i=0; i<iterations; i+=4) { y1 = _mm_set_ss(output[i]); y2 = _mm_set_ss(output[i+1]); y3 = _mm_set_ss(output[i+2]); y4 = _mm_set_ss(output[i+3]); for(k=0; k<ksize; k++){ for(l=0; l<ksize; l++){ w = _mm_set_ss(weight[i+k+l]); x1 = _mm_set_ss(input[i+k+l]); y1 = _mm_add_ss(y1,_mm_mul_ss(w,x1)); … x4 = _mm_set_ss(input[i+k+l+3]); y4 = _mm_add_ss(y4,_mm_mul_ss(w,x4)); } } _mm_store_ss(&output[i],y1); _mm_store_ss(&output[i+1],y2); _mm_store_ss(&output[i+2],y3); _mm_store_ss(&output[i+3],y4); } 我知道我可以使用打包的fp向量来提高性能,而且我已经成功地做到了这一点,但是我想知道为什么单个标量代码无法满足处理器的峰值性能。 这个内核在我的机器上的性能是每个周期约1.6个FP操作,而每个周期最多可以有2个FP操作(因为FP + FP […]

Angular JSresize的div指令

我的网站将有多个部分,我打算可以resize。 为了做到这一点,我做了一个“可resize”的指令,例如: <div class="workspace" resize="full" ng-style="resizeStyle()"> <div class="leftcol" resize="left" ng-style="resizeStyle()"> 有一个像这样的指令: lwpApp.directive('resize', function ($window) { return { scope: {}, link: function (scope, element, attrs) { scope.getWinDim = function () { return { 'height': window.height(), 'width': window.width() }; }; // Get window dimensions when they change and return new element dimensions // based on attribute scope.$watch(scope.getWinDim, […]

你将如何做相当于Python中的预处理器指令?

有没有办法在Python中执行以下预处理器指令? #if DEBUG < do some code > #else < do some other code > #endif

Angularjs – 将parameter passing给指令

我想知道是否有办法将parameter passing给一个指令? 我想要做的就是像这样在控制器中添加一个指令: $scope.title = "title"; $scope.title2 = "title2"; angular.element(document.getElementById('wrapper')).append('<directive_name></directive_name>'); 是否有可能在同一时间传递参数,以便我的指令模板的内容可以链接到一个或另一个范围? 这里是指令: app.directive("directive_name", function(){ return { restrict:'E', transclude:true, template:'<div class="title"><h2>{{title}}</h3></div>', replace:true }; }) 如果我想使用相同的指令,但是使用$ scope.title2,该怎么办?

将数组通过属性传递给AngularJS指令

我目前有一个问题时,通过该指令的属性传递一个指令的指令。 我可以读取它作为一个string,但我需要它作为一个数组,所以这是我想出来,但它不工作。 帮助任何人? 提前 使用Javascript :: app.directive('post', function($parse){ return { restrict: "E", scope:{ title: "@", author: "@", content: "@", cover: "@", date: "@" }, templateUrl: 'components/postComponent.html', link: function(scope, element, attrs){ scope.tags = $parse(attrs.tags) } } } HTML :: <post title="sample title" tags="['HTML5', 'AngularJS', 'Javascript']" … >

Angularjs从$ http自动完成

我试图写一个自动完成指令,使用$ http请求从服务器获取数据(不使用任何外部插件或脚本) 。 目前它只适用于静态数据。 现在,我知道我需要将我的$ http请求插入到指令的source:中,但是我找不到任何关于这个主题的好文档。 http请求 $http.post($scope.url, { "command": "list category() names"}). success(function(data, status) { $scope.status = status; $scope.names = data; }) . error(function(data, status) { $scope.data = data || "Request failed"; $scope.status = status; }); 指示 app.directive('autoComplete', function($timeout) { return function(scope, iElement, iAttrs) { iElement.autocomplete({ source: scope[iAttrs.uiItems], select: function() { $timeout(function() { iElement.trigger('input'); […]