Продолжалось это некоторое время; По сути, у меня есть директива, которая открывает ngDialog, эта директива должна иметь возможность принимать переменную из корневой области. Директива в основном имеет событие щелчка, которое открывает ngDialog, этот ngDialog затем использует переданное значение и устанавливает его как текст текстового поля... после обновления текстового поля в ngDialog оно должно отражать изменение в корневой области.
Моя проблема. Передаваемое значение не связано с корневой областью, после обновления значения в ngDialog оно не отражается обратно в корневую область. Я почти уверен, что только что совершил основную ошибку. Кто-нибудь может помочь?
// HTML
<b>Instructions: </b>Click on the blue items to open ngDialog<br /><br />
<div ng-controller="MyCtrl">
<b>Base $scope.variable1 = </b> {{variable1}}
<span pass-object passed-object="variable1"></span><br />
<b>Base $scope.variable2 = </b> {{variable2}}
<span pass-object passed-object="variable2"></span><br />
<b>Base $scope.variable3 = </b> {{variable3}}
<span pass-object passed-object="variable3"></span><br />
</div>
//Js
var myApp = angular.module('myApp',['ngDialog']);
myApp.controller('MyCtrl', function ($scope) {
//Lets say i have 3 scope variables
$scope.variable1 = "value 1";
$scope.variable2 = "value 2";
$scope.variable3 = "value 3";
});
//Now i want to create a directive that opens up a ngdialog, I need to be able to pass in a scope variable into this directive
//and from inside the ngDialog i need to be able to update the variable passed in, and have it reflect from the root scope.
myApp.directive('passObject', ['ngDialog', function(ngDialog) {
return {
restrict: 'A',
scope: { passedObject: '=' },
template: "<div class='directive'>This is the value passed into this directive = {{passedObject}}!</div>",
link: function($scope, element){
element.on('click',function(){
ngDialog.open({
template: '<div>By updating i need it to reflect in the root scope:<br /><br />' +
'<input type="text" ng-model="passedObject"/></div>',
plain: true,
scope: $scope,
controller: ['$scope', function($scope){
$scope.$watch('passedObject', function(passedObject){
//What do i need to do? it seems like the scope at this level is updating how come the parent is not?
alert('updated with: ' + passedObject);
$scope.$apply();
});
}]
})
});
}
};
}]);
//Скрипка
https://jsfiddle.net/Egli/od8a2hL0/
//Благодарность :D
Заранее спасибо