我有一个创建按钮的指令。单击该按钮时,它应更新父作用域中的变量firstName
。我已经在我的控制器中设置了$scope.firstName = "Test"
。
指令:
myApp.directive('myComponent', function(){
return {
scope: {
firstName: '='
},
link: function(scope, element, attrs){
// A function that should update a parent scope variable.
scope.changeName = function(){
scope.firstName = "Name was changed."
}
}),
template: '<button ng-click="changeName">Test</button>'
};
});
单击按钮时,firstName应在此处更改,但不会。 为什么firstName不会更新?
<div>
{{ firstName }}
<my-component first-name="firstName"></my-component>
</div>
答案 0 :(得分:0)
我认为您在ng-click上错过了'()'。
myApp.directive('myComponent', function(){
return {
scope: { firstName: '=' },
template: '<button ng-click="changeName()">Test</button>',
link: function(scope, element, attrs){
// A function that should update a parent scope variable.
scope.changeName = function(){
scope.firstName = "Name was changed."
}
}
}
});
无论如何,我已经做了一个小提琴只是为了帮助你。检查它,它工作正常。如果您有任何其他疑问,请回复我。链接到小提琴: