gpt4 book ai didi

AngularJS模态窗口指令

转载 作者:行者123 更新时间:2023-12-04 04:53:31 25 4
gpt4 key购买 nike

我正在尝试为Twitter Bootstrap Modal创建指令angularJS指令。

    var demoApp = angular.module('demoApp', []);

demoApp.controller('DialogDemoCtrl', function AutocompleteDemoCtrl($scope) {
$scope.Langs = [
{Id:"1", Name:"ActionScript"},
{Id:"2", Name:"AppleScript"},
{Id:"3", Name:"Asp"},
{Id:"4", Name:"BASIC"},
{Id:"5", Name:"C"},
{Id:"6", Name:"C++"}
];

$scope.confirm = function (id) {
console.log(id);
var item = $scope.Langs.filter(function (item) { return item.Id == id })[0];
var index = $scope.Langs.indexOf(item);
$scope.Langs.splice(index, 1);
};
});

demoApp.directive('modal', function ($compile, $timeout) {
var modalTemplate = angular.element("<div id='{{modalId}}' class='modal' style='display:none' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'><div class='modal-header'><h3 id='myModalLabel'>{{modalHeaderText}}</h3></div><div class='modal-body'><p>{{modalBodyText}}</p></div><div class='modal-footer'><a class='{{cancelButtonClass}}' data-dismiss='modal' aria-hidden='true'>{{cancelButtonText}}</a><a ng-click='handler()' class='{{confirmButtonClas}}'>{{confirmButtonText}}</a></div></div>");
var linkTemplate = "<a href='#{{modalId}}' id= role='button' data-toggle='modal' class='btn small_link_button'>{{linkTitle}}</a>"

var linker = function (scope, element, attrs) {
scope.confirmButtonText = attrs.confirmButtonText;
scope.cancelButtonText = attrs.cancelButtonText;
scope.modalHeaderText = attrs.modalHeaderText;
scope.modalBodyText = attrs.modalBodyText;
scope.confirmButtonClass = attrs.confirmButtonClass;
scope.cancelButtonClass = attrs.cancelButtonClass;
scope.modalId = attrs.modalId;
scope.linkTitle = attrs.linkTitle;

$compile(element.contents())(scope);
var newTemplate = $compile(modalTemplate)(scope);

$(newTemplate).appendTo('body');

$("#" + scope.modalId).modal({
backdrop: false,
show: false
});
}

var controller = function ($scope) {
$scope.handler = function () {
$timeout(function () {
$("#"+ $scope.modalId).modal('hide');
$scope.confirm();
});
}
}

return {
restrict: "E",
rep1ace: true,
link: linker,
controller: controller,
template: linkTemplate
scope: {
confirm: '&'
}
};
});​

这是JsFiddle示例 http://jsfiddle.net/okolobaxa/unyh4/15/

但是handler()函数的运行次数与页面上的指令一样多。为什么?正确的方法是什么?

最佳答案

Bootstrap3的AngularStrap中有一个有效的 native 实现,它利用了AngularJS v1.2 +中的ngAnimate

  • 演示:http://mgcrea.github.io/angular-strap/##modals

  • 您可能还想结帐:
  • 来源:https://github.com/mgcrea/angular-strap/blob/master/src/modal/modal.js
  • Plunkr:http://plnkr.co/edit/vFslNmBAoKPVXtdmBXgv?p=preview
  • 关于AngularJS模态窗口指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13414049/

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