gpt4 book ai didi

javascript - 在 AngularJS 中将 angular.element() 转换为模板

转载 作者:行者123 更新时间:2023-12-02 17:34:09 25 4
gpt4 key购买 nike

我正在尝试将当前使用 angular.element() 的指令转换为模板,但到目前为止,模板还无法正常工作。作为 AngularJS 的新手,我认为我犯了一些大错误,但到目前为止我还看不到它。有什么指点吗?

原文如下:

app.directive('theDirective',  ['musicInfoService',
function(musicInfoService) {
return {
restrict: 'A',
scope: { position: '@', last: '@', release: '=',
artist: '=', versions: '@' },
link: function(scope, element, attrs) {

element.bind('click', function() {
// Highlight clicked element
angular.element(document.querySelector('.clicked')).removeClass('clicked');
element.addClass('clicked');

// Create the collapse element or select existing one
var collapseQuery = document.querySelector('#collapse');
var collapse = collapseQuery === null ?
angular.element('<div id="collapse" ng-controller="VersionController" class="col-md-12 col-xs-12"> \
<div class="inner"> \
<ul> \
<li class="title">{{release.title}}</li> \
<li class="row top" ng-show="versions"> \
<div class="col-md-1">Year</div> \
<div class="col-md-3">Format</div> \
<div class="col-md-3">Label</div> \
<div class="col-md-2">Country</div> \
<div class="col-md-2">Cat. Nº</div> \
</li> \
<li class="row" ng-show="versions" ng-repeat="version in versions | filter: \'!file\' | orderBy: version.released"> \
<div class="col-md-1">{{(version.released | release:4) || \'-----\'}}</div> \
<div class="col-md-3">{{version.format}}</div> \
<div class="col-md-3">{{version.label}}</div> \
<div class="col-md-2">{{version.country}}</div> \
<div class="col-md-2">{{version.catno}}</div> \
</li> \
<li class="row top" ng-hide="!release.format"> \
<div class="col-md-1">Year</div> \
<div class="col-md-3">Format</div> \
<div class="col-md-3">Label</div> \
</li> \
<li class="row" ng-hide="!release.format"> \
<div class="col-md-1">{{release.year}}</div> \
<div class="col-md-3">{{release.format}}</div> \
<div class="col-md-8">{{release.label}}</div> \
</li> \
</ul> \
</div> \
</div>') :
angular.element(collapseQuery);

// Based on the position of the clicked element calculate the rounded number up to the nearest multiple of four
var calculatedPosition = Math.ceil(scope.position / 4) * 4;

// Get the element at the calculated position or the last one
var calculatedQuery = document.querySelector('[position="' + calculatedPosition + '"]');
if (calculatedQuery === null) calculatedQuery = document.querySelector('[last="true"]');;

var calculatedElement = angular.element(calculatedQuery);

// Insert the collapse element after the element at the calculated position
calculatedElement.after(collapse);
// Highlight the calculated element
angular.element(document.querySelector('.calculated')).removeClass('calculated');
calculatedElement.addClass('calculated');
});

scope.$on('$destroy', function() {
element.unbind('click');
});
}
};
}
]);

转换后的:

  app.directive('theDirective',  ['musicInfoService',
function(musicInfoService) {
return {
restrict: 'A',
scope: { position: '@', last: '@', release: '=', artist: '='},
template: '<div id="collapse" ng-controller="VersionController" class="col-md-12 col-xs-12"> \
<div class="inner"> \
<ul> \
<li class="title">{{release.title}}</li> \
<li class="row top" ng-show="versions"> \
<div class="col-md-1">Year</div> \
<div class="col-md-3">Format</div> \
<div class="col-md-3">Label</div> \
<div class="col-md-2">Country</div> \
<div class="col-md-2">Cat. Nº</div> \
</li> \
<li class="row" ng-show="versions" ng-repeat="version in versions | filter: \'!file\' | orderBy: version.released"> \
<div class="col-md-1">{{(version.released | release:4) || \'-----\'}}</div> \
<div class="col-md-3">{{version.format}}</div> \
<div class="col-md-3">{{version.label}}</div> \
<div class="col-md-2">{{version.country}}</div> \
<div class="col-md-2">{{version.catno}}</div> \
</li> \
<li class="row top" ng-hide="!release.format"> \
<div class="col-md-1">Year</div> \
<div class="col-md-3">Format</div> \
<div class="col-md-3">Label</div> \
</li> \
<li class="row" ng-hide="!release.format"> \
<div class="col-md-1">{{release.year}}</div> \
<div class="col-md-3">{{release.format}}</div> \
<div class="col-md-8">{{release.label}}</div> \
</li> \
</ul> \
</div> \
</div>',
replace: false,
link: function(scope, elem, attrs) {

elem.bind('click', function() {
// Highlight clicked element
elem(document.querySelector('.clicked')).removeClass('clicked');
elem.addClass('clicked');

// Create the collapse element or select existing one
var collapseQuery = document.querySelector('#collapse');
var collapse = collapseQuery === null ?
elem(collapseQuery)

// Based on the position of the clicked element calculate the rounded number up to the nearest multiple of four
var calculatedPosition = Math.ceil(scope.position / 4) * 4;

// Get the element at the calculated position or the last one
var calculatedQuery = document.querySelector('[position="' + calculatedPosition + '"]');
if (calculatedQuery === null) calculatedQuery = document.querySelector('[last="true"]');;

var calculatedElement = angular.element(calculatedQuery);

// Insert the collapse element after the element at the calculated position
calculatedElement.after(collapse);

});

scope.$on('$destroy', function() {
elem.unbind('click');
});
}
};
}
]);

编辑:该指令来自 this Plunker .

编辑 II:这是 the example我正在尝试实现它。

最佳答案

    var collapse = collapseQuery === null ?
elem(collapseQuery)

三元运算符中缺少“else”替代项。还有分号

    var collapse = collapseQuery === null ?
elem(collapseQuery) : "Some other option";

你的 Controller 代码非常困惑,我放弃了阅读它。尝试以更“有 Angular ”的方式定义它。

app.controller('Ctrl', ["$scope" , "musicInfoService", 
function ($scope, musicInfoService) {
$scope.getDetails = function (id) {
// Your function goes here.
};

$scope.search = function () {
musicInfoService.doSomething();
};
}
]);

但这可能不是问题所在。你有

ng-controller="VersionController"

我没有看到它的任何定义。就像 Ctrl 一样,VersionController 也需要一个定义。您得到的错误非常清楚。

错误:[ng:areq] 参数“VersionController”不是函数,未定义

关于javascript - 在 AngularJS 中将 angular.element() 转换为模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22773916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com