如何将我选中的项目存储在带有其他控制器的复选框中?
我的尝试(请参阅plnkr查看视图):
var myApp = angular.module('myApp', []);
myApp.factory('CooSelection', function () {
return {selectedCoo: []}
})
function CooListCtrl($scope, CooSelection) {
$scope.coos = {"Coos": ["spark", "nark", "hark", "quark"]};
$scope.coo_list_selection = CooSelection;
$scope.checkSelection = function (item) {
if ($scope.coo_list_selection.indexOf(item) === -1) {
$scope.coo_list_selection.push(item);
} else {
$scope.coo_list_selection.splice($scope.coo_list_selection.lastIndexOf(item), 1);
}
}
}
CooListCtrl.$inject = ['$scope', 'CooSelection'];
function DebugCooList($scope, CooSelection) {
$scope.coo_selection = CooSelection;
}
DebugCooList.$inject = ['$scope', 'CooSelection'];
Run Code Online (Sandbox Code Playgroud) javascript angularjs angularjs-directive angularjs-scope angularjs-controller
我的HTML中有一个" ng-init "指令,它在我的Angular.js数据模型中初始化" data.id " 的值.我们现在说,我无法改变其运作方式.
现在,我希望在页面加载后立即发出HTTP请求,其中URL将取决于此data.id值.到目前为止,以下似乎有效:
app.controller('MainCtrl', function ($scope, $http, $timeout) {
"use strict";
$scope.data = {};
$timeout(function () {
$http.get("/api/Something?id=" + $scope.data.id);
}, 0);
});
Run Code Online (Sandbox Code Playgroud)
但是,使用超时为零的计时器似乎很笨拙.(如果我省略$ timeout代码并直接调用$ http.get,那么$ scope.data.id当然是未定义的).
在发出$ http请求之前,有没有更好的方法等待ng-init执行?以上基于超时的代码是否适用于所有情况/所有浏览器等?
我是AngularJS的新手.使用数组参数声明的控制器之间的区别是什么,将依赖关系列为字符串和JavaScript名称,
app.controller("firstController", ['$scope', '$modal', '$log', 'HttpService', 'FisrtSharedService', 'SecondSharedService', function($scope, $modal, $log, HttpService, FisrtSharedService, SecondSharedService) {
}]);
Run Code Online (Sandbox Code Playgroud)
...和这个表单,只列出JavaScript名称?
app.controller("firstController", function($scope, $modal, $log, HttpService, FisrtSharedService, SecondSharedService){
});
Run Code Online (Sandbox Code Playgroud)
为什么第一个版本中的奇怪语法?
我已经看到很多这些问题,但没有找到有效的解决方案.这是一个小提琴,不起作用但应该.
http://jsfiddle.net/cdparmeter/j2K7N/2/
控制器:
$scope.foo = function (textArray) {
console.log(textArray)
};
Run Code Online (Sandbox Code Playgroud)
指示:
return {
restrict: 'E',
replace: 'true',
scope: {
methodToCall: '&method'
},
template: "<div>
<input ng-model='textToPush'/>
<button ng-click='pushText'>Push</button>
<button ng-click='finish'>Finish</button>
</div>",
link: function (scope, element, attrs) {
scope.paragraphs = [];
scope.pushText = function () {
scope.paragraphs.push(scope.pushText);
scope.pushText = "";
}
scope.finish = function () {
scope.methodToCall(scope.paragraphs)
}
}
}
Run Code Online (Sandbox Code Playgroud)
HTML:
<div ng-app="MyApp">
<div ng-controller="MyController">
<container data-method="foo">
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我正在我的指令中构建一个数组,需要在父作用域的控制器中进行自定义处理.我知道我可以在我传递给我的指令的模型上的父范围内抛出一块手表,但这看起来很黑而且很脏.有什么建议?
javascript angularjs angularjs-directive angularjs-controller
我在上搞清楚如何从我的传递参数的麻烦angular controller来
service
#my controller
'use strict';
angular.module('recipeapp')
.controller('recipeCtrl', ['$scope', 'recipeService',
function($scope, recipeService){
$scope.recipeFormData={};
$scope.recipeSave = function(){
recipeService.saveRecipe();
}
}]);
#my service
'use strict';
angular.module('recipeapp').service('recipeService',['$http', function($http){
this.saveRecipe = save;
function save(callback){
//calling external http api
}
}]);
Run Code Online (Sandbox Code Playgroud)
我在这里尝试做的是,$scope.formData从我的表单和控制器获取应该传递给service,根据我的理解,我不能$scope在里面使用service所以我需要找到一种传递$scope.formData给服务的方式
在控制器中,强硬的想法会是,recipeService.saveRecipe($scope.formData);但我不确定如何从服务中收集,
当我改变服务this.saveRecipe(val) = save;它不起作用:(
任何帮助都会得到满足
angularjs angularjs-service angularjs-scope angularjs-controller
为什么我无法绑定到第二个控制器内的控制器变量?
<div ng-app="ManagerApp">
<div ng-controller="MainCtrl">
Main:
<div ng-repeat="state in states">
{{state}}
</div>
<div ng-controller="InsideCtrl as inside">
Inside:
<div ng-repeat="state in inside.states2">
{{state}}
</div>
</div>
</div>
</div>
var angularApp = angular.module('ManagerApp', []);
angularApp.controller('MainCtrl', ['$scope', function ($scope) {
$scope.states = ["NY", "CA", "WA"];
}]);
angularApp.controller('InsideCtrl', ['$scope', function ($scope) {
$scope.states2 = ["NY", "CA", "WA"];
}]);
Run Code Online (Sandbox Code Playgroud)
示例:https://jsfiddle.net/nukRe/135/
第二次ng-repeat不起作用.
binding controller angularjs angularjs-controller angularjs-controlleras
请原谅我,如果这听起来很愚蠢,但我现在已经使用AngularJS一段时间了,我看到有人告诉我将我的逻辑包装在一个指令(或服务?)而不是我的控制器中,只保留我控制器中的绑定.除了指令的可重用性方面还有其他原因吗?
到目前为止,我还没有真正理解为什么会这样.不写指令会带来很多开销吗?我没有遇到任何在我的控制器中编写逻辑的问题,而且很容易.这种方法的缺点是什么?
standards angularjs angularjs-directive angularjs-controller
我正在研究角度教程,我在开始时遇到了问题.加载myApp模块会引发错误.如教程中所述,这应该是创建控制器的三种方法之一.
这是我正在研究的教程的打印屏幕:

当我刷新网页时,我在Chrome控制台中收到此错误:
Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:nomod] Module 'myApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
Run Code Online (Sandbox Code Playgroud)
这是我的HTML文件
<html ng-app="myApp">
<head>
</head>
<body>
<h1>Hello world!</h1>
<div ng-controller="MainController">
{{ 2+2 }}
<br>
{{ val }}
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script>
<script src="app.js">
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是我的app.js文件
var myApp = angular.module('myApp', []);
var MainController = function($scope){
$scope.val …Run Code Online (Sandbox Code Playgroud) html javascript angularjs angularjs-directive angularjs-controller
我在angularJS中创建了一个服务,它使用btford.socket-io模块与服务器进行交互.因为在服务中我实现了一些我目前在angular内部使用的API,但是为了以后扩展应用程序,我还需要在角度范围之外访问这些API.因此,将来可以在不需要创建控制器和其他东西的情况下调用该函数.
目前我为控制器做了这个
var myController = angular.element($('body')).scope().myController;
Run Code Online (Sandbox Code Playgroud)
将整个控制器保存在范围变量中.我想知道是否可以对服务做同样的事情.
javascript socket.io angularjs angularjs-service angularjs-controller
有没有办法知道哪些依赖项被注入我的Angular模块?
angular.module('myModule', [
'ui.bootstrap'
])
.controller('myController', [function () {
// var dependencies = Magic.dependencies;
// console.log(dependencies);
}]);
Run Code Online (Sandbox Code Playgroud)