已经解决了一段时间了;基本上,我有一个打开ngDialog的指令,该指令需要能够从根范围获取变量。
该指令基本上具有一个click事件,可打开一个ngDialog,然后将该ngDialog使用传入的值并将其设置为
文本框的文本... ngDialog中的文本框更新后,它将反映出根作用域上的更改。
我的问题
传入的值未链接到rootscope,一旦在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();
});
}]
})
});
}
};
}]);
//小提琴
http://jsfiddle.net/Egli/od8a2hL0/
//感谢
:D
先谢谢了
最佳答案
控制台显示$ digest已经在进行中,只需删除$scope.$apply();
http://jsfiddle.net/usmoetyd/
您必须使用对象而不是原始类型:
myApp.controller('MyCtrl', function ($scope) {
//Lets say i have 3 scope variables
$scope.variable1 = { value : "value 1" };
$scope.variable2 = { value: "value 2" };
$scope.variable3 = { value: "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 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.value}}!</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.value"/></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?
console.log(passedObject);
});
}]
})
});
}
};
}]);
http://jsfiddle.net/jyk6Lbwe/1/
阅读此问题以获取详细说明:What are the nuances of scope prototypal / prototypical inheritance in AngularJS?
关于javascript - AngularJs:将值传递给打开ngDialog的指令,然后使值更新反射(reflect)在根范围内,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26375309/