用户点击输入字段后..我想获取输入字段的ID&过滤对象&绑定Popover窗口上的数据。但Scope没有从指令更新。以下是代码,请让我知道我在哪里做错了
var app = angular.module('MyModule', []);
app.controller("TestController", function ($scope, $window) {
//$scope.ID = '0';
$scope.myObj = [{ "ID": 0, "Name": 'user0' }, { "ID": 1, "Name": 'user1' }, { "ID": 2, "Name": 'user2' }]
$scope.GetData = function () {
var match = $.grep($scope.myObj, function (e) {
return e.ID == $scope.myID
});
return match;
};
});
app.directive('popOver', function ($compile, $timeout) {
return {
restrict: 'A',
link: function (scope, el, attrs) {
$(el).bind('click', function () {
scope.$apply(function () {
scope.myID = attrs.popoverid ;
});
});
$(el).popover({
placement: 'bottom',
container: 'body',
trigger: "click",
content: $compile($('#popover-content').html())(scope)
});
}
};
});
<div ng-repeat="i in [0,1,2]">
<input style="background: transparent"
type="text"
pop-over
popoverid="{{i}}"
id="{{i}}"
class="form-control enterTime" data-html="true">
<br /><br />
</div>
<div class="bottom hide" id="popover-content">
<div class="arrow" style="left: 47%;"></div>
<div class="Bground-Project">
<table>
<tr>
<td>
{{GetData()}}
</td>
</tr>
</table>
<button type="button" ng-click="buttonClicked()">click me</button>
</div>
</div>
答案 0 :(得分:0)
当写入基本类型(即字符串,数字或布尔类型)时 - 例如,scope.name = newName - “write”始终转到本地范围/对象。换句话说,子作用域获取自己的名称属性,该属性隐藏同名的父属性。修复是在父作用域中使用对象而不是基本类型。然后,子范围将获得对该对象的引用。对对象属性的任何写入(无论是来自父对象还是子对象)都将转到该对象。 (子范围没有自己的对象。)
Is it Possible to Update Parent Scope from Angular Directive with scope: true?