【发布时间】:2015-09-24 05:44:24
【问题描述】:
自定义指令代码如下:
angular.module('app.directives', ['ng']).directive('liveSearch', [
'$compile', '$timeout', function($compile, $timeout) {
return {
restrict: 'E',
replace: true,
require: 'ngModel',
scope: {
ngModel: '=',
liveSearchCallback: '=',
liveSearchSelect: '=?',
liveSearchItemTemplate: '@',
liveSearchWaitTimeout: '=?',
liveSearchMaxResultSize: '=?',
liveSearchResultField: '=?'
},
template: "<input type='text' />",
link: function(scope, element, attrs, controller) {
scope.$watch('selectedIndex', function(newValue, oldValue) {
var item;
item = scope.results[newValue];
if (item) {
scope.ngModel = '10';
element.val(item[attrs.liveSearchResultField]
}
});
}
}}]);
这不是我的指令的完整代码,但足以理解问题。另请查看代码:
<live-search class="form-control" id="search1" live-search-callback="mySearchCallback" live-search-result-field="title"ng-model="skill" type="text"></live-search>
Value: {{ skill }}
<button class="btn btn-success" type="submit" ng-click="createEmployee">Сохранить</button>
我的控制器代码:
$scope.createEmployee = function() {
console.log($scope.skill);
};
如您所见,我的自定义指令具有名为“skill”的变量名称的“ng-model”属性。如何更改自定义指令中的“技能”值?我的方法行不通。提前致谢!
【问题讨论】:
标签: javascript angularjs