如何从另一个控制器更新控制器的ng-repeat模型?(How to update the ng-repeat model of controller from another controller?)
我有两个带有自己控制器的
<div>
。 第一个div有一个ng-model="listEntries"
。 我在这个<div>
的控制器中初始化listEntries
。app.controller('firstController', function($scope,serviceForFirst){ serviceForFirst.init(); serviceForFirst.getList($scope); });
HTML
<div ng-controller="firstController"> <ul> <li ng-repeat="each in listEntries"> {{each.name}} </li> <ul> </div>
我将
$scope
传递给getList()
并在serviceForFirst
设置$scope.listEntries
值。 然后我使用listEntries
作为ng-model
。app.service('serviceForFirst',function(){ var list=[]; var init=function(){ list = [{....}]; }; var getList=function($scope){ $scope.listEntries = list; }; var update=function(newEntity){ list.push(newEntity); }; return{ init:init, getList:getList, update:update }; });
这是我的第二个控制器和与之相关的服务。 我打算每次调用
addNew()
都将新元素推送到listAll
。 这就是我试图去做的。app.controller('secondController', function($scope,serviceForSecond){ serviceForSecond.init(); $scope.addNew=function(newEntity){ serviceForSecond.addNew(newEntity); }; }); app.service('serviceForSecond',function(serviceForFirst){ var entities=[]; var init=function(){ entities=[{....}]; }; var addNew=function(newEntity){ entities.push(newEntity); serviceForFirst.update(newEntity); return{ init:init, addNew:addNew }; });
这个
<div>
的HTML<div ng-controller="secondController"> .... <input type="text" ng-model="newName"/> <button ng-click="addNew(newName)"/> .... </div>
但是列表没有在第一个
<div>
更新。 如果我在设置$scope.listEntries
之前试图在getList()
设置$scope.$apply()
,那么我得到$ digest已经在进行错误。当我做
console.log()
,我发现每个服务中的适当函数都被调用,但是列表没有被更新。我应该如何更新清单?
I have two
<div>
with their own controllers. The first div has ang-model="listEntries"
. I initializelistEntries
in this<div>
's controller.app.controller('firstController', function($scope,serviceForFirst){ serviceForFirst.init(); serviceForFirst.getList($scope); });
Html
<div ng-controller="firstController"> <ul> <li ng-repeat="each in listEntries"> {{each.name}} </li> <ul> </div>
I pass the
$scope
to thegetList()
and set the$scope.listEntries
value inserviceForFirst
. I then uselistEntries
asng-model
.app.service('serviceForFirst',function(){ var list=[]; var init=function(){ list = [{....}]; }; var getList=function($scope){ $scope.listEntries = list; }; var update=function(newEntity){ list.push(newEntity); }; return{ init:init, getList:getList, update:update }; });
This is my second controller and the service associated to it. I intend to push new elements into
listAll
every time I invokeaddNew()
. This is how I'm trying to do it.app.controller('secondController', function($scope,serviceForSecond){ serviceForSecond.init(); $scope.addNew=function(newEntity){ serviceForSecond.addNew(newEntity); }; }); app.service('serviceForSecond',function(serviceForFirst){ var entities=[]; var init=function(){ entities=[{....}]; }; var addNew=function(newEntity){ entities.push(newEntity); serviceForFirst.update(newEntity); return{ init:init, addNew:addNew }; });
The HTML for this
<div>
<div ng-controller="secondController"> .... <input type="text" ng-model="newName"/> <button ng-click="addNew(newName)"/> .... </div>
But the list is not getting updated in the first
<div>
. If I try to do$scope.$apply()
ingetList()
before setting$scope.listEntries
then I get $digest already in progress error.When I do
console.log()
, I see that the appropriate function in each the services are getting invoked but the list is not being updated.How should I update the list?
原文:https://stackoverflow.com/questions/28905111
最满意答案
var array = string.split(',');
Use the String.split()
var array = string.split(',');
相关问答
更多-
相关的代码 // If no match was found, return this if (off == 0) return new String[]{this}; // Add remaining segment if (!limited || list.size() < limit) list.add(substring(off, value.length)); relavant co ...
-
此行为在String.split(String regex) (强调我的)中明确记录: 该方法的工作原理是通过使用给定的表达式和极限参数为零调用双参数拆分方法。 因此, 尾随的空字符串 不会包含在结果数组中。 如果您希望包含尾随的空字符串,则需要对第二个参数( limit )使用负值的String.split(String regex, int limit) limit ): String[] array = values.split("\\|", -1); This behavior is explic ...
-
你可以分割一个空字符串: var chars = "overpopulation".split(''); 如果您只想以类似阵列的方式访问字符串,则可以无需split : var s = "overpopulation"; for (var i = 0; i < s.length; i++) { console.log(s.charAt(i)); } 您还可以使用正常的数组语法访问其索引的每个字符。 但是请注意,字符串是不可变的,这意味着您无法使用此方法设置字符的值,并且它不受IE7的支持(如果仍 ...
-
字符串拆分比较[重复](String split comparison [duplicate])[2022-02-28]
请记住,当您在java中使用字符串比较时使用“a.equals(b)” list.get(0).equals(x[0]); Remember that when you use String comparisons in java to use "a.equals(b)" list.get(0).equals(x[0]); -
以下是一些选项: 1. String.Split with char和String.Trim 使用string.Split ,然后修剪结果以删除多余的空格。 public string[] info13 = info12.Split(',').Select(str => str.Trim()).ToArray(); 请记住, Select需要using System.Linq; 2. String.Split与char数组 不需要修剪,虽然这种方法不是我的最爱 public string[] info13 ...
-
您可以使用空字符串作为分隔符的.split() 。 这将在每个字符处拆分字符串: function nextBigger(num){ console.log(num.toString().split("")); } nextBigger(513); You can just .split() with an empty string as the delimiter. This will split the string at each character: function nextBigg ...
-
如何拆分字符串并使用jquery插入数组[复制](How to split string and push in array using jquery [duplicate])[2022-02-10]
使用String.split() var array = string.split(','); Use the String.split() var array = string.split(','); -
最多25个字符的示例,您可以使用此模式: /\S[\s\S]{0,23}\S(?=\s|$)/g 演示 代码示例: var text = " I am totally unappreciated in my time. You can run this whole park from this room with minimal staff for up to 3 days. You think that kind of automation is easy? Or cheap? You know any ...
-
使用正则表达式模式时不要使用引号。 请改用以下内容 var array = str.split(/:|am|AM|pm|PM/g); 以下是您的完整示例: function splitTime(str) { var array = str.split(/:|am|AM|pm|PM/g); console.log("[0]: " + array[0] + " [1]: " + array[1] + " [2]: " + array[2]); } splitTime('12 ...
-
我有一个对象 这不是一个对象,它是一个类。 (好的,被授予,它也是一个对象。)要访问items ,首先需要一个实例来访问它: const instanceOfFoo: Foo = new Foo(); 然后,一旦你有了这个实例,并且你有split的数组,你可以: 替换 Foo实例上的数组: instanceOfFoo.items = splitted; 要么 将这些添加到您的项目中(例如,如果您可以使用相同的Foo多次执行此操作): instanceOfFoo.items.push(...splitte ...