Javascript 角度:检索指令中的更新值
我正在用数据填充名为commentRecipe.html的模板 我可以从模板内部调用controller.add(1,2,'comment here') 该项在数据库中得到更新并返回我的新结果 问题是:如何使用检索到的数据更新ex-mhRecipeIdJavascript 角度:检索指令中的更新值,javascript,angularjs,Javascript,Angularjs,我正在用数据填充名为commentRecipe.html的模板 我可以从模板内部调用controller.add(1,2,'comment here') 该项在数据库中得到更新并返回我的新结果 问题是:如何使用检索到的数据更新ex-mhRecipeId var app = angular.module('myApp'); app.directive('mhCommentRecipe', ['$log', 'commentService', function ($log, commentServ
var app = angular.module('myApp');
app.directive('mhCommentRecipe', ['$log', 'commentService', function ($log, commentService) {
var commentController = function () {
var that = this;
that.add = function (commentId, recipeId, commentText) {
if (recipeId && commentText.length > 0) {
var resultObj = commentService.add(commentId, recipeId, commentText);
}
};
}
return {
restrict: 'A',
templateUrl: '/app/templates/commentRecipe.html',
scope: {
mhRecipeId: '=',
mhCommentId: '=',
mhCommentText: '='
},
controller: commentController,
controllerAs: 'controller'
}
}]);
(function () {
app.factory('commentService', [
'$log', 'httpService', function ($log, httpService) {
var baseEndpointPath = '/myRecipes';
return {
add: function (commentId, recipeId, commentText) {
$log.debug(commentId, 'c');
var data = {
commentId,
recipeId,
commentText
}
var promise = httpService.post(baseEndpointPath + '/comment', data);
promise.then(function (response) {
// added
},
function (error) {
$log.error(error);
});
},
remove: function (commentId) {
if (commentId) {
var data = {
commentId,
recipeId,
commentText
}
data.commentId = commentId;
var promise = httpService.post(baseEndpointPath + '/removeComment', data);
promise.then(function (response) {
$log(response, 'removed response');
},
function (error) {
$log.error(error);
});
}
}
};
}
]);
})();
app.factory('httpService', ['$http', '$q',
function ($http, $q) {
return {
get: function (endpoint) {
var deferred = $q.defer();
$http.get(endpoint)
.success(function (response) {
deferred.resolve(response);
})
.error(function () {
deferred.reject('Failed to fetch response from endpoint');
});
return deferred.promise;
},
post: function (endpoint, data, config) {
var deferred = $q.defer();
$http.post(endpoint, data, config)
.success(function (response) {
deferred.resolve(response);
})
.error(function () {
deferred.reject('Failed to post data to endpoint');
});
return deferred.promise;
},
put: function (endpoint, data, config) {
var deferred = $q.defer();
$http.put(endpoint, data, config)
.success(function (response) {
deferred.resolve(response);
})
.error(function () {
deferred.reject('Failed to put data to endpoint');
});
return deferred.promise;
}
};
}]);
将要发送到指令的值放入变量中:
// in controller
that.mhRecipeId = whateverValueHere;
that.mhCommentId = whateverValueHere;
that.mhCommentText = 'comment text';
然后在html中的指令上,您将:
<mh-comment-recipe mh-recipe-id="controller.mhRecipeId" mh-comment-id="controller.mhCommentId" mh-comment-text="controller.mhCommentText"></mh-comment-recipe>
这将把变量传递到指令中以供使用
除非我误解了你的问题:)解决了问题,我很不好意思告诉你答案!
在我看来,当我应该使用controllers值时,我只是使用了ex:mRecipeId。我想你可能不理解我的问题,更新了我的文本以使其更清楚。你说你的服务返回了一个新结果。所以,用新检索到的数据填充that.mbRecipeId。我注意到服务正在使用中,但没有分配给任何对象。服务是否返回结果?如果是这样,您可以
var result=commentService.add(commentId、recipeId、commentText)代码>很抱歉TjGinger是,它返回一个对象。正在更新我的代码。Tj Gienger你能给我看看吗?我试着这么做,但没有成功。你的评论服务。添加返回承诺吗?Darren,没有,它返回承诺的结果。下次发布你的所有代码。这本可以更快地解决。