我做了一个自定义指令, 在模板中有一个变量ng-model =" test",
在指令控制器中, 如何对变量进行任何更改?
angular.module("myDirective", []).directive("ngTest", function(){
return {
restrict: "E",
scope: true,
template: "<input type='text' ng-model='test'>+
<button ng-click='change()'></button>",
controller: function ($scope, $element){
$scope.change = function(){
// no idea about how to control variable test here.
$scope.test = "123123123"; //this doesn't work. :(
}
}
}
});
答案 0 :(得分:2)
以下是您的工作plunker:)
app.directive("ngTest", function(){
return {
restrict: "E",
scope: true,
template: "<input type='text' ng-model='test'><button ng-click='change()'>Click Me</button>",
controller: function ($scope, $element){
$scope.change = function(){
// no idea about how to control variable test here.
$scope.test = "123123123"; //this doesn't work. :(
}
}
}
});
我发现问题是指令中的'+'不起作用你需要在同一行提供完整的tempalte,否则使用tempalteUrl为它提供值。