【发布时间】:2023-03-21 00:50:01
【问题描述】:
我们的网站上有这个按钮:
<button class="btn btn-primary" id="button-loading-example"
ng-click="progress.buttonLoading.load()"
ng-model="progress.buttonLoading" pb-button-progress
type="button">Save</button>
与此相关的 ng-model,在控制器中(控制器为“进度”):
_this.buttonLoading = {
isLoading: false,
text: 'Saving',
load: function() {
_this.buttonLoading.isLoading = true;
$timeout(function() {
_this.buttonLoading.isLoading = false;
}, 2000);
}
};
我正在尝试将此作为指令。我可以从我的元素中传递类和标签:
<pb-spinner-button ng-class-list="btn btn-primary">Save</pb-spinner-button>
我不知道如何连接(或简单地丢弃和取代)ng-model,更糟糕的是,我不知道在哪里挂接旧控制器中的“加载”功能。
我意识到旧的控制很可能是过度的,应该是指令,但我继承的人是指令厌恶的。
到目前为止,这是我的指令:
(function() {
'use strict';
angular.module('app').directive('pbSpinnerButton', function() {
return {
restrict: 'E',
require: 'ngModel',
scope: {
ngModel: '=',
ngClassList: '@'
},
controller: 'SpinnerController',
template: '<button class="{{ngClassList}}" ng-click="buttonLoading.load()" ng-model="buttonLoading" pb-button-progress type="button"><ng-transclude></button>',
transclude: true,
link: function postLink(scope, element, attrs) {
}
};
})
.controller('SpinnerController', function($timeout) {
var _this = this;
_this.buttonLoading = {
isLoading: false,
text: 'Saving',
load: function() {
_this.buttonLoading.isLoading = true;
$timeout(function() {
_this.buttonLoading.isLoading = false;
}, 2000);
}
};
});
})();
根据要求,这里是一个 plunkr:http://plnkr.co/edit/EOekzFdUDrArZ4dkolLl?p=preview
【问题讨论】:
-
没有模型被传递给属性,但是因为它是一个按钮,你需要 ng-model 吗? .. 在 plunker 中创建一个演示。
-
这是我不明白的一部分。这是一个笨蛋plnkr.co/edit/EOekzFdUDrArZ4dkolLl?p=preview
-
抛出的错误是由于没有传递 ng-model。对于
type=button,我看不到 ng-model 有任何用处……只是一个 UI 按钮 -
也不是使用
ng-前缀的好习惯...暗示核心指令 -
我现在看到的主要问题是控制器正在使用
controllerAs范围,但您尚未在指令中声明controllerAs。 Simple demo 获取加载函数以创建警报:
标签: javascript angularjs angularjs-directive components