我尝试在内部编辑后在我的控制器中运行一个功能来更新我的基础数据,但它没有触发......无法理解原因。如何检查我是否在合适的范围内?我使用Meanjs
作为测试,我试图在我的控制器中触发函数testar()。
angular.module('customers').directive('editInPlace', [
function() {
return {
restrict: 'E',
scope: {
value: '='
},
template: '<span ng-click="edit()" ng-bind="value"></span><input ng-model="value"></input>',
link: function (scope, element, attrs) {
// Let's get a reference to the input element, as we'll want to reference it.
var inputElement = angular.element(element.children()[1]);
// This directive should have a set class so we can style it.
element.addClass('edit-in-place');
// Initially, we're not editing.
scope.editing = false;
// ng-click handler to activate edit-in-place
scope.edit = function () {
scope.editing = true;
// We control display through a class on the directive itself. See the CSS.
element.addClass('active');
// And we must focus the element.
// `angular.element()` provides a chainable array, like jQuery so to access a native DOM function,
// we have to reference the first element in the array.
inputElement[0].focus();
};
// When we leave the input, we're done editing.
inputElement.prop('onblur', function () {
scope.editing = false;
element.removeClass('active');
console.log("trigg");
scope.$apply("testar()");
});
}
};
}
]);
angular.module('customers').controller('CustomersController', ['$scope', '$stateParams', '$location', 'Authentication', 'Customers',
function($scope, $stateParams, $location, Authentication, Customers ) {
$scope.authentication = Authentication;
// Create new Customer
$scope.create = function() {
// Create new Customer object
var customer = new Customers ({
name: this.name,
firstName:this.firstName
});
$scope.testar = function () {
console.log("jojjemen från controller");
}
}
]);
答案 0 :(得分:0)
添加到您的范围:
scope
{
value: '=',
controllerFunction: '='
}
在您的链接功能上,您可以致电:
scope.controllerFunction();
然后在HTML上,当你包含你的指令时,你需要将控制器函数作为属性传递:
<edit-in-place value="something" controller-function="testar">