【发布时间】:2017-07-22 11:47:34
【问题描述】:
我是 AngularJS 的新手,对指令性的东西有点困惑。
在下面的代码中,我需要使用外包模块中的指令创建一个输入字段,并使用我的自定义指令关注它。
小心我不能更改外包模块...
myHtml.html
<body ng-app="main" ng-controller="myCtrl">
<my-field></my-field>
</body>
Outsource.js
var app2 = angular.module('pain', []);
app2.directive('myField', [function() {
return {
scope.f = 'focus-me';
},
template: 'name: <input type="text" class="{{f}}"/>' +
'<br/>' +
'input class is: \"{{f}}\"'
}
}]);
myApp.js
var app1 = angular.module('main', ['pain']);
app1.controller('myCtrl', ['$scope', function($scope) {
$scope.state = true;
}]);
app1.directive('focusMe', ['$timeout', '$parse', function($timeout, $parse) {
return {
restrict: 'C',
link: function(scope, element) {
scope.$watch(scope.state, function() {
console.log('value=', scope.state);
if (scope.state === true) {
$timeout(function() {
element[0].focus();
}, 20);
}
});
}
};
}]);
小提琴
fiddler
可以使用下面这段代码,但我无法更改外包代码。
它可以工作
fiddler
【问题讨论】:
标签: javascript angularjs angularjs-directive fiddler directive