gpt4 book ai didi

javascript - 具有 javascript 模型和 ngModel 的 Angular 组件

转载 作者:行者123 更新时间:2023-11-28 06:40:41 26 4
gpt4 key购买 nike

我正在尝试使用纯 JavaScript 模型创建一个 Angular 组件,一个时间选择器,我希望组件的 Controller 公开一个 api 并与 ngModel 一起使用。我是 Angular 的新手,不知道如何使用 ngModel。我在模板中有两个输入,分别是小时和分钟。我的问题是我不知道如何将 ngmodel 参数传递给 Controller ​​。

我准备了一个plunker: http://plnkr.co/edit/aal3VP?p=preview

(function() {
var app = angular.module('plunker', []);

function DemoController() {
this.tpVal = {
hours: 10,
minutes: 0
};
}
app.controller('DemoController', DemoController);

function TimePickerModel(config) {

this.show = config.show || true;

this.hours = null;

this.minutes = null;

}

function TimePickerController() {

// API for state
this.model = new TimePickerModel({});
}

TimePickerController.prototype.show = function showTimePicker() {
this.model.show = true;
};

TimePickerController.prototype.hide = function hideTimePicker() {
this.model.show = false;
};

TimePickerController.prototype.setHours = function setHoursTimePicker(hours) {
this.model.hours = hours;
};

TimePickerController.prototype.setMinutes = function setMinutesTimePicker(minutes) {
this.model.minutes = minutes;
};

TimePickerController.prototype.setValue = function setValueTimePicker(value) {
this.model.hours = value;
this.model.minutes = value;
};

app.directive('timepicker', function($compile) {

return {
restrict: 'AE',
controller: 'TimePickerController',
scope: {},
require: 'ngModel',
templateUrl: 'timepicker.html',
link: function(scope, element, attrs, ngModel) {
//console.log('Model val: ' + ngModel.$modelValue);
//console.log('View val: ' + ngModel.$viewValue);
ngModel.$render = function() {
//Do something with your model
console.log(scope.model);
var actualValue = ngModel.$modelValue;
console.log('Model val: ' + ngModel.$modelValue.hours);
console.log('View val: ' + ngModel.$viewValue.hours);
//console.log(element.find('input')[0]);
//element.find('input')[0].val(actualValue.hours);
}
}
};
});

app.controller('TimePickerController', TimePickerController);

})();
<!DOCTYPE html>
<html ng-app="plunker">

<head>
<script data-require="angular.js@1.4.7" data-semver="1.4.7" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script>
<script data-require="angular.js@1.4.7" data-semver="1.4.7" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-route.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="app.js"></script>
</head>

<body>
<h1>Hello Plunker!</h1>
<div ng-controller="DemoController as ctrl">
{{ctrl.tpVal}}
<timepicker ng-model="ctrl.tpVal"></timepicker>

</div>

</body>

</html>

最佳答案

ng-modelstandard angular directive要将输入值绑定(bind)到范围属性,您不需要注入(inject)它或调用具有相同名称的指令属性。如果您想将 Controller 中的值注入(inject)指令中,可以使用范围属性。在指令中:

scope: {
model : '=time'
},

在index.html

   <timepicker time="ctrl.tpVal"></timepicker>

检查修改:http://plnkr.co/edit/cJ0mjI?p=preview 。您还可以通过在指令中添加虚拟 increaseHours 函数来了解指令内部模型值的更改如何传播到外部;

关于javascript - 具有 javascript 模型和 ngModel 的 Angular 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33853803/

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