我对Angular指令还很陌生,正在尝试实现自己想要的功能时遇到了很多问题。以下是我的基本代码:
控制器:
controller('profileCtrl', function($scope) {
$scope.editing = {
'section1': false,
'section2': false
}
$scope.updateProfile = function() {};
$scope.cancelProfile = function() {};
});
指令:
directive('editButton', function() {
return {
restrict: 'E',
templateUrl: 'editbutton.tpl.html',
scope: {
editModel: '=ngEdit'
}
};
});
模板(editbutton.tpl.html):
<button
ng-show="!editModel"
ng-click="editModel=true"></button>
<button
ng-show="editModel"
ng-click="updateProfile(); editModel=false"></button>
<button
ng-show="editModel"
ng-click="cancelProfile(); editModel=false"></button>
HTML:
<edit-button ng-edit="editing.section1"></edit-button>
如果不清楚,我希望
<edit-button>
标签包含三个不同的按钮,每个按钮与传递到ng-edit
中的作用域属性进行交互。当单击时,它们应更改该属性,然后调用相应的作用域方法。现在的情况是,单击按钮会正确地更改
$scope.editing
的值,但updateProfile
和cancelProfile
方法不起作用。我可能完全错了如何正确使用指令,但我很难找到一个在线示例来帮助我完成我想要做的事情。任何帮助都将不胜感激。
$parent
可能不再引用控制器作用域,而是某些其他中间作用域。 - Jerradupdate-profile='updateProfile({message: testMessage})'
。 - Jerrad